0
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
4

1 回答 1

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
于 2012-09-06T09:27:55.540 回答