【问题标题】:How to read a NSMutableArray out of a NSMutableDictionary如何从 NSMutableDictionary 中读取 NSMutableArray
【发布时间】:2011-09-02 00:47:53
【问题描述】:

我在从可变字典中读取可变数组时遇到问题。我正在尝试将其读出,然后将其放入标签中。这是我的数据的组织方式。我有一个可变的字典数组。然后在我的表格视图中,我在索引路径中获取对象并将其设置为等于应用程序委托中的字典。然后在详细信息视图中,我加载从数组中取出的字典并从中读取并将值放入标签中。我的字典中的所有字符串都在工作,但数组不会读出,你能帮帮我吗

RootViewController.m

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SurveyAppAppDelegate *surveyAppAppDelegate = (SurveyAppAppDelegate*)[[UIApplication sharedApplication] delegate];

    SurveyViewer *surveyViewController = [[SurveyViewer alloc] initWithNibName:@"SurveyViewer" bundle:nil]; 
    surveyAppAppDelegate.arrayCount = [[self surveyArray] objectAtIndex:indexPath.row];


    [self.navigationController pushViewController:surveyViewController animated:YES];
    [surveyViewController release];
}

在我的 App Delegate 中,它被设置为 NSDictionary。

然后在我的详细视图控制器中执行此操作

  injuredArray = [surveyAppAppDelegate.arrayCount objectForKey:@"Injured Part"];

    if ([injuredArray count] >= 1) {
        for (int i = 0; i<=([injuredArray count]-1); i++) {
            myLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 105+shift, 139, 15)];
            myLabel.text = [injuredArray objectAtIndex:i];
            myLabel.backgroundColor = [UIColor clearColor];
            myLabel.font = [UIFont fontWithName:@"Helvetica" size: 14.0];
            myLabel.textColor = [UIColor blackColor];
            myLabel.textAlignment = UITextAlignmentLeft;
            [self.theScroller addSubview:myLabel];
            shift = shift+20;
        }
    }

在我的 App Delegate 中,我像这样设置了 arrayCount

.h

#import <UIKit/UIKit.h>

    @class RootViewController;

    @interface SurveyAppAppDelegate : NSObject <UIApplicationDelegate> {
        NSDictionary *arrayCount;
    }

    @property (nonatomic, assign) NSDictionary *arrayCount;

    @end

和.m

#import "SurveyAppAppDelegate.h"
@implementation SurveyAppAppDelegate
    @synthesize arrayCount;

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {


        arrayCount = [[NSDictionary alloc] init];

然后在 app delegate 的 dealloc 中我释放它

哦,还有我设置为injuredArray的NSMutableArray中的对象都是字符串

编辑:所以我在设置后立即在根视图控制器中的 NSLog 数组中添加了一个循环,但它不起作用。所以它看起来由于某种原因它没有正确保存在 arrayCount 中

感谢您的帮助

【问题讨论】:

  • 在您的详细视图控制器中,对surveyAppAppDelegate 的引用来自哪里?你确定它不为空吗?
  • 是的,我将它设置为 NSDictionary,做属性和综合,然后我像这样初始化它 arrayCount = [[NSDictionary alloc] init];
  • 受伤数组的值是多少?

标签: iphone objective-c nsmutablearray nsmutabledictionary


【解决方案1】:

在您的 appDelegate 标头中,尝试保留字典而不是分配:

@property (nonatomic, retain) NSDictionary *arrayCount;

然后在实施中:

NSDictionary *anArrayCount = [[NSDictionary alloc] init];
self.arrayCount = anArrayCount;
[anArrayCount release];

您分配的受伤数组对象是否已经分配和初始化?这可能是问题所在......

【讨论】:

  • 感谢您的回答,我认为问题在于我将一个可变数组放入字典之类的东西中,我不是 100% 确定,我所知道的是经过一些修补后它现在可以工作了
  • 我后来又遇到了一些相同类型的问题,通过保留字典解决了,我本来打算像往常一样保留它,一定是错字:/ Don'你讨厌那些,因为当它是正确的语法并且不会显示错误消息时,它们很难捕捉到
【解决方案2】:

您可以考虑在详细视图控制器中创建一个 init 方法,该方法接受您感兴趣的 NSDictionary 并将其保留在属性中。我不知道这个参考来自哪里:surveyAppAppDelegate.arrayCount

【讨论】:

  • 是的,我做到了,您可以查看原帖,我发布了我的应用委托代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多