gpt4 book ai didi

ios - JSON 写入错误 - 处理自定义类时类型无效

转载 作者:行者123 更新时间:2023-11-28 21:50:22 25 4
gpt4 key购买 nike

我有许多自定义类,它们是 NSObject 的子类,我正在尝试将它们写入 JSON 文件。我的顶级类 Survey 是一个单例,它的一个属性是 addressArray,它包含另一个名为 Address 的类的实例。

Survey 的 addressArray 中可以有任意数量的地址,我正在尝试将所有数据写入一个 JSON 文件。我的代码如下:

//In both Survey.m and Address.m, I have a method like this: 
- (NSDictionary *)surveyDictionaryRepresentation
{
NSMutableDictionary *dictionary = [NSMutableDictionary new];
dictionary[@"name"] = [self name] ? : [NSNull null];
dictionary[@"emailAddress"] = [self emailAddress] ? : [NSNull null];
dictionary[@"addressArray"] = [self addressArray] ? : [NSNull null];
dictionary[@"storage"] = [NSNumber numberWithInteger:[self storage]] ? : [NSNull null];
dictionary[@"extraStops"] = [NSNumber numberWithBool:[self extraStops]] ? : [NSNull null];

return [NSDictionary dictionaryWithDictionary:dictionary];
}

然后,在 View Controller 中,我在 ViewDidLoad 方法中有以下内容。

NSArray *surveys = @[[[Survey sharedInstance] surveyDictionaryRepresentation]];
NSMutableArray *addresses = [[NSMutableArray alloc]init];
for (Address *address in [[Survey sharedInstance]addressArray]){
//What the hell was I thinking?
[addresses addObject:[addressaddressDictionaryRepresentation]];
}
NSDictionary *dictionary = @{@"Survey": surveys, @"Address":addresses};
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:nil];
NSLog(@"%@", [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]);

现在,我收到以下错误: 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“JSON 写入(地址)中的类型无效”

我明白为什么会有问题了。因为包含 Address 实例的 addressArray 在 Survey 中,所以将 Address 数据添加到为 Survey 创建的 JSON 对象时似乎存在问题。我不确定如何解决这个问题。在使用断点逐步执行程序后,我发现崩溃是在它尝试执行以下行后发生的:NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:nil];

对于它的值(value),我在循环遍历地址数组并对它的内容运行 addressesDictionaryRepresentation 方法后对地址数组进行了 NSLog,它完美地记录,完美地显示所有属性和所有值。我只是无法将它放入 JSON 文档中。有什么建议么?非常感谢您的帮助。

编辑:AddressArray 日志的前几行:

  2015-02-18 11:21:29.122 [70958:920733] (
{
aCCOI = "-1";
aCfloorNumber = "<null>";
activity = 0;
addressType = "-1";
allowedToReserveDock = "-1";

编辑 2:Survey.m 中唯一的其他代码是建立单例的代码: + (实例类型)sharedInstance { 静态调查 *_instance; 静态 dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _instance = [[调查分配] init]; }); 返回实例;

编辑3: 如果([自地址数组]){ NSMutableArray *addresses = [[NSMutableArray alloc]init]; 对于(地址 * [[Survey sharedInstance]addressArray] 中的地址){ [地址addObject:[地址addressDictionaryRepresentation]]; } dictionary[@"addressArray"] = [self addressArray]; } 别的 { dictionary[@"addressArray"] = [NSNull null];

最佳答案

[self addressArray] 应该返回一个 JSON 数组,而不是带有 Address 对象的数组。就像您对 @"Address"字段所做的那样:
NSMutableArray *addresses = [[NSMutableArray alloc]init];
for (Address *address in [[Survey sharedInstance]addressArray]){
[addresses addObject:[addressaddressDictionaryRepresentation]];<br/>
}

返回 NSMutableArray 地址,以便将其设置为 @"addressArray"的值。那你应该没问题。

关于ios - JSON 写入错误 - 处理自定义类时类型无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28588123/

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