Reputation: 171
I get an array from a JSON and I parse it into an NSMutableArray
(this part is correct and working). I now want to take that array and print the first object to a Label. Here is my code:
NSDictionary *title = [[dictionary objectForKey:@"title"] objectAtIndex:2];
arrayLabel = [title objectForKey:@"label"];
NSLog(@"arrayLabel = %@", arrayLabel); // Returns correct
//Here is where I need help
string = [arrayLabel objectAtIndex:1]; //I do not get the first label (App crashes)
NSLog(@"string = %@", string);
other things that I have already tried are as follows:
string = [NSString stringWithFormat:@"%@", [arrayImage objectAtIndex:1]];
and
string = [[NSString alloc] initWithFormat:@"%@", [arrayImage objectAtIndex:1]];
Any help is greatly appriciated!
EDIT: The app does not return a single value and crashes.
Upvotes: 0
Views: 52
Reputation: 90711
In addition to whatever else was going on, you repeatedly refer to "first" but use the index 1. In most C-based programming languages (and others, as well) the convention is that indexes into arrays are 0-based. So, use index 0 to get the first element.
Upvotes: 1
Reputation: 81142
Your code doesn't match the structure of your JSON. In your comment on the deleted answer, you said you got an exception when sending objectAtIndex:
to an NSString
. In your case, arrayLabel
isn't an array when you think it is.
If your JSON has an object, your code needs to treat it as an NSDictionary
. Likewise for arrays and NSArray
and strings and NSString
.
Upvotes: 1