NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:responseData
options:kNilOptions
error:&error];
NSArray* users = [json objectForKey:@"Users"];
NSEnumerator* enumerator = [users objectEnumerator];
id element;
NSMutableArray *results;
Result *fetchedResults;
while(element = [enumerator nextObject]) {
// fetchedResults = [[Result alloc] init]; // i have tried commenting/uncommenting
fetchedResults.name = (NSString *)[[element objectForKey:@"User"] objectForKey:@"name"];
fetchedResults.email = (NSString *)[[element objectForKey:@"User"] objectForKey:@"name"];
NSLog(@"%@", fetchedResults.name);
[results addObject:fetchedResults];
NSLog(@"%@", (NSString *)[[element objectForKey:@"User"] objectForKey:@"name"]); // this returns valid dump
}
NSLog(@"%d", [results count]); // returns 0
我不明白这里错了。我搜索了大量的教程和资源似乎没有在这里弄错了。
编辑:
NSLog(@"%@", fetchedResults.name); // dumps null
答案 0 :(得分:1)
您忘记分配结果数组NSMutableArray *results = [[NSMutableArray alloc] init]
这应该有帮助。
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:responseData
options:kNilOptions
error:&error];
NSArray* users = [json objectForKey:@"Users"];
NSMutableArray *results = [[NSMutableArray alloc] init];
for (id object in users) {
Result *fetchedResults = [[Result alloc] init];
fetchedResults.name = (NSString *)[[element objectForKey:@"User"] objectForKey:@"name"];
fetchedResults.email = (NSString *)[[element objectForKey:@"User"] objectForKey:@"name"];
NSLog(@"%@", fetchedResults.name);
[results addObject:fetchedResults];
}
NSLog(@"%@", (NSString *)[[element objectForKey:@"User"] objectForKey:@"name"]);
}
NSLog(@"%d", [results count]); // returns 0