如何从NSMutableDictionary中读取NSMutableArray

时间:2011-09-01 15:53:24

标签: iphone objective-c nsmutablearray nsmutabledictionary

我在从可变字典中读出可变数组时遇到问题。我想把它读出来,然后把它放到标签上。以下是我的数据组织方式。我有一个可变的字典数组。然后在我的tableview中,我将索引路径中的对象设置为等于应用委托中的字典。然后在详细信息视图中,我加载了从数组中取出的字典,然后从中读取并将值放入标签中。我在字典中的所有字符串都有效,但是数组不会读出来,你可以帮我吗

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委托的dealloc中我释放它

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

编辑:所以我在设置它之后在根视图控制器中添加了一个循环到NSLog数组,它不能正常工作。所以它找出了一些原因,它没有正确保存在arrayCount中

感谢您的帮助

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

@property (nonatomic, retain) NSDictionary *arrayCount;

然后在实施中:

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

您指定的damagedArray对象是否已分配并初始化?那可能是问题......