从模板创建对象崩溃并显示错误:"NSInvalidArgumentException"



此函数旨在获取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编程的新手,不知道这意味着什么。

 NSArray *parsedObject = [NSJSONSerialization JSONObjectWithData:objectNotation options:0 error:&localError];

如果您不关心生成的字符串的可读性,则传递 0

如果要将对象解析为 NSArray,请将选项更改为 NSJSONReadingMutableContainers

NSDictionary *dic_JSON = [NSJSONSerialization JSONObjectWithData: jsonData
                                options: NSJSONReadingMutableContainers
                                  error: &error];

相关内容

最新更新