0

我对 ios dev 非常陌生,到目前为止,我几乎一直在努力解决所有问题。这真的很令人沮丧。

我已将注释/代码崩溃放在这里/以指示我的应用程序流停止的位置。我正在请求 json 数据

NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://192.168.2.4/iRestaurant/users"]];
    [self performSelectorOnMainThread:@selector(fetchedData:)
                           withObject:data waitUntilDone:YES];

fetchedData 的实现

- (void)fetchedData:(NSData *)responseData {
//parse out the json data
NSError* error;
NSDictionary* json = [NSJSONSerialization
                      JSONObjectWithData:responseData //1

                      options:kNilOptions
                      error:&error];
//NSLog(@"%@", json);
NSArray* latestLoans = [json objectForKey:@"Users"]; //2
NSEnumerator* enumerator = [latestLoans objectEnumerator];
id element;

  while(element = [enumerator nextObject]) {
    Result *fetchedResults = [Result init];
    /*crashes here*/
    fetchedResults.name = [[element objectForKey:@"User"] objectForKey:@"name"]; 
    fetchedResults.email = [[element objectForKey:@"User"] objectForKey:@"name"];
    [results addObject:fetchedResults];objectForKey:@"name"]);
  }
}

Result是一个自定义对象,是 NSObject 的子类。我已经导入了 Result.h。我还在当前文件的声明文件中声明了 NSMutableArray *results。我正在将对象推入results。结果的头文件看起来像

@interface Result : NSObject

@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *email;
@property (nonatomic, assign) int ID;

@end

我将实现 cellForRowAtIndexPath 并将结果的内容推送到可重用的单元格中。

我的 JSON 响应是

{"Users":
    [{"User":
        {"id":"1",
         "name":"Kishor kundan",
         "email":"kis@kun.ca",
         "password":"asdfasdf",
         "fb_id":"1234444"
        }
     },
     {"User":         
        {"id":"2",
         "name":"adsfasdf",
         "email":"asdfasdf@asdf.asdf",
         "password":"asdfasdf",
         "fb_id":"123123"
     }
    }]
}
4

1 回答 1

0

由于找不到对象,代码崩溃了。我的代码引用了一个不存在的位置。我从另一个问题中接受了@btype 的建议

于 2012-09-20T18:42:34.470 回答