此函数用于获取JSON并根据作为参数发送的对象创建对象数组:
+ (NSArray *)Object: (id) object FromJSON:(NSData *)objectNotation error:(NSError **)error
{
NSError *localError = nil;
NSArray *parsedObject = [NSJSONSerialization JSONObjectWithData:objectNotation options:0 error:&localError];
if (localError != nil) {
*error = localError;
return nil;
}
NSMutableArray *output = [[NSMutableArray alloc] init];
NSArray *results = [parsedObject valueForKey:@"results"];
NSLog(@"Count %lu", (unsigned long)results.count);
for (NSArray *eventDic in results) {
object = [[[object class] alloc] init];
for (NSString *key in eventDic) {
if ([object respondsToSelector:NSSelectorFromString(key)]) {
[object setValue:[eventDic valueForKey:key] forKey:key];
}
}
[output addObject:object];
}
return output;
}
但是每次运行它都会崩溃。我收到这个错误:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSNull countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x1100c3ce0'
我是iOS编程新手,不知道这意味着什么。
答案 0 :(得分:0)
NSArray *parsedObject = [NSJSONSerialization JSONObjectWithData:objectNotation options:0 error:&localError];
如果您不关心生成的字符串的可读性,则传递0
如果要将对象解析为NSArray,请将选项更改为NSJSONReadingMutableContainers
NSDictionary *dic_JSON = [NSJSONSerialization JSONObjectWithData: jsonData
options: NSJSONReadingMutableContainers
error: &error];