1

I have a function meant for an iOS app. The NSString valueand 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;
}
Dhwanit Zaveri
  • 465
  • 1
  • 5
  • 15
  • 2
    Beginner mistake. Unlike most other languages, in Objective-C making a call on a nil pointer does not cause an error but simply returns zero/nil. – Hot Licks Jul 21 '13 at 12:11
  • Related: [Where's the difference between setObject:forKey: and setValue:forKey: in NSMutableDictionary?](http://stackoverflow.com/a/1249653/335858) – Sergey Kalinichenko Jul 21 '13 at 12:15

1 Answers1

2

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];
Tim
  • 8,932
  • 4
  • 43
  • 64