将值分配给可变字典中的键

时间:2013-07-21 12:00:42

标签: ios nsmutabledictionary

我有一个适用于iOS应用的功能。 NSString valuekey包含值和键,但它似乎无法分配到data行上的NSMutableDictionary [data setValue:value forKey:key]

-(NSMutableDictionary *)parseCode:(NSString *)string{
    NSArray *array = [string componentsSeparatedByString:@"||"];
    NSMutableDictionary *data;
    for(id object in array){
        NSArray *objectAndKey = [object componentsSeparatedByString:@"::"];
        NSString *key =[objectAndKey objectAtIndex:0];
        NSString *value = [objectAndKey objectAtIndex:1];
        [data setValue:value forKey:key];
        [self alertMeWithString:[data valueForKey:key]];
    }
    return data;
}

1 个答案:

答案 0 :(得分:2)

您需要初始化并分配NSMutableDictionary。

 NSMutableDictionary *data = [NSMutableDictionary dictionary];

或者您可以使用

明确拨打电话
 NSMutableDictionary *data = [[NSMutableDictionary alloc] init];