I am trying to get an array full of my data, I keep getting an BAD_ACCESS error when I run this though at the calling the array which I have not included here but I even commented that code out and tried just calling it to the log and still get the BAD_ACCESS error. The array is stored in a dictionary that contains a one key that is a number. I am not sure what I am doing wrong here.
ISData *is = [[ISData alloc] init];
NSDictionary *dic = [is getData:@"right" : isNumber];
NSArray *array = [[NSArray alloc] initWithArray:[dic valueForKey:@"2"]];
NSString *out = [array objectAtIndex:0];
How the dictionary is created:
NSNumber* key = [NSNumber numberWithInt:isNumber];
NSArray *values = [NSArray arrayWithObjects:[NSString stringWithUTF8String:name], [NSString stringWithUTF8String:desc], [NSString stringWithUTF8String:intent], nil];
[dic setObject:values forKey:key];
You don’t say exactly where it crashes, and you don’t have an obvious crashing bug here, so it’s hard to diagnose your actual issue. This could be a memory management thing that’s outside the code you’ve presented. But a couple things are going on here that are suspicious:
You should never have a bare
[MyClass alloc]without the-initcall. Your init should call super’s init, which is responsible for setting up the new object.Your
-valueForKey:should be-objectForKey:. The difference is probably unimportant in this case, but the former is used for “KVC” coding, which you’re not using. If you set it as object, get it as object.Your
@"2"as the key into the dictionary doesn’t match your input, which is an NSNumber. NSNumbers are not string versions of numbers, so you’re unlikely to find any value there. Instead, use the same[NSNumber numberWithInt:2]pattern.