gpt4 book ai didi

ios - 从模板创建对象崩溃并出现错误 : "NSInvalidArgumentException"

转载 作者:行者123 更新时间:2023-12-02 03:43:03 26 4
gpt4 key购买 nike

此函数旨在获取 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];

关于ios - 从模板创建对象崩溃并出现错误 : "NSInvalidArgumentException",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27007137/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com