简体   繁体   中英

Assigning values to keys in a mutable dictionary

I have a function meant for an iOS app. The NSString value and key contain the value and key however it doesnt seem to get assign to the NSMutableDictionary data on the line [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;
}

You need to initialise and allocate your NSMutableDictionary.

 NSMutableDictionary *data = [NSMutableDictionary dictionary];

Or you can explicitly make the calls using

 NSMutableDictionary *data = [[NSMutableDictionary alloc] init];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM