2012-07-24 154 views
0

行,所以我填充数组是这样的:空NSMutableArray里,不知道为什么

NSMutableArray *participants; 
for(int i = 0; i < sizeofpm; i++){ 
     NSDictionary *pmpart_dict = [pm_participants objectAtIndex:i]; 
     NSString *pmpart_email = [pmpart_dict objectForKey:@"email"]; 
     NSString *pmpart_email_extra = [@"pm" stringByAppendingString:pmpart_email]; 
     [participants setValue:pmpart_email forKey:pmpart_email_extra]; 
     NSLog(@"%@", participants); 
    } 

sizeofpm是1,即使用计数。获取数组中的值的数量。我如何将值存储到该数组?它似乎没有工作。谢谢!

回答

2

需要先ALLOC它。尝试的第一行更改为:

NSMutableArray* participants = [[NSMutableArray alloc] init];

也使用与NSMutableArraysetValue:forKey:不会工作作为数组没有密钥。

尝试使用[participants addObject:pmpart_email];

+2

我错过了,你错过了的setValue形式的字典:!在我们之间,我们已经涵盖了它。 – jrturton 2012-07-24 13:54:16

+0

你们很棒.. <3谢谢! – jimbob 2012-07-24 15:25:56

2

不创建数组,你只需要声明它。

NSMutableArray *participants = [NSMutableArray array]; 

之后,setValue:forKey:不会将对象添加到数组。您需要addObject:

[participants addObject:pmpart_email]; 

没有关键。

1

要分配怎么样,你赋值给一个NSDictionary对象的值到NSMutableArray *participants。要将值分配给NSMutableArray,您可以拨打- (void)addObject:(id)anObject

0

因此,我作为其他答案中的一些人指出,您缺少participants的初始值设定项。但是,您使用的setValue:forKey:判断,以及如何你似乎是结构化数据,你不找NSMutableArray,而是NSMutableDictionary。数组只是列表,而字典则保持键值关系,您似乎试图利用这些关系。

试试这个:

// some classes provide shorthand for `alloc/init`, such as `dictionary` 
NSMutableDictionary *participants = [NSMutableDictionary dictionary]; 
for(int i = 0; i < sizeofpm; i++){ 
    NSDictionary *pmpart_dict = [pm_participants objectAtIndex:i]; 
    NSString *pmpart_email = [pmpart_dict objectForKey:@"email"]; 
    NSString *pmpart_email_extra = [@"pm" stringByAppendingString:pmpart_email]; 
    [participants setValue:pmpart_email forKey:pmpart_email_extra]; 
    NSLog(@"%@", participants); 
} 

这会给你的

{ 
    pmpart_email_extra: pmpart_email 
} 
相关问题