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 に答える