ios - Obj-C NSMutableArray 返回 nil
问题描述
我正在尝试执行一个简单的任务。我有一个 json 格式的字典数组。我想从中创建一个对象数组。
+ (NSArray<BuyableUnit*>*)mapUnits:(NSArray<NSDictionary*>*)obj {
NSMutableArray *units = [[NSMutableArray alloc] init];
for (id each in obj) {
BuyableUnit *unit = [self mapUnit:each];
[units addObject:unit];
}
return [units copy];
}
+ (BuyableUnit*)mapUnit:(NSDictionary*)obj {
BuyableUnit *unit = [[BuyableUnit alloc] init];
[unit setType:obj[@"type"]];
[unit setUnit:obj[@"unit"]];
return unit;
}
我得到的错误:
-[BuyableUnit encodeWithCoder:]: unrecognized selector sent to instance 0x60c000293240
2018-06-15 12:51:08.140404-0400 MyApp[20978:20764150] *** -[NSKeyedArchiver dealloc]: warning: NSKeyedArchiver deallocated without having had -finishEncoding called on it.
2018-06-15 12:51:08.152077-0400 MyApp[20978:20764150] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[BuyableUnit encodeWithCoder:]: unrecognized selector sent to instance 0x60c000293240'
*** First throw call stack:
(
0 CoreFoundation 0x00000001119631e6 __exceptionPreprocess + 294
1 libobjc.A.dylib 0x0000000110ff8031 objc_exception_throw + 48
2 CoreFoundation 0x00000001119e4784 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
3 CoreFoundation 0x00000001118e5898 ___forwarding___ + 1432
4 CoreFoundation 0x00000001118e5278 _CF_forwarding_prep_0 + 120
5 Foundation 0x000000010dbd05f0 _encodeObject + 1197
6 Foundation 0x000000010dbd1af0 -[NSKeyedArchiver _encodeArrayOfObjects:forKey:] + 439
7 Foundation 0x000000010dbd05f0 _encodeObject + 1197
8 Foundation 0x000000010dc02e3a +[NSKeyedArchiver archivedDataWithRootObject:] + 156
9 CoreData 0x000000010cd9d764 -[NSSQLiteConnection execute] + 2084
10 CoreData 0x000000010cef54a1 -[NSSQLiteConnection updateRow:forRequestContext:] + 817
11 CoreData 0x000000010cfbf03b _writeChangesForSaveRequest + 1419
12 CoreData 0x000000010cfc0886 _executeSaveChangesRequest + 390
13 CoreData 0x000000010ce0f812 -[NSSQLSaveChangesRequestContext executeRequestCore:] + 18
14 CoreData 0x000000010cf8f52c -[NSSQLStoreRequestContext executeRequestUsingConnection:] + 204
15 CoreData 0x000000010cf6469b __52-[NSSQLDefaultConnectionManager handleStoreRequest:]_block_invoke + 75
16 libdispatch.dylib 0x0000000112e69848 _dispatch_client_callout + 8
17 libdispatch.dylib 0x0000000112e705b8 _dispatch_queue_barrier_sync_invoke_and_complete + 374
18 CoreData 0x000000010cf64580 -[NSSQLDefaultConnectionManager handleStoreRequest:] + 336
19 CoreData 0x000000010cf6c324 -[NSSQLCoreDispatchManager routeStoreRequest:] + 308
20 CoreData 0x000000010cec2b05 -[NSSQLCore dispatchRequest:withRetries:] + 229
21 CoreData 0x000000010cebecb1 -[NSSQLCore processSaveChanges:forContext:] + 193
22 CoreData 0x000000010cda4373 -[NSSQLCore executeRequest:withContext:error:] + 787
23 CoreData 0x000000010cea568f __65-[NSPersistentStoreCoordinator executeRequest:withContext:error:]_block_invoke + 2015
24 CoreData 0x000000010ce9d7a8 -[NSPersistentStoreCoordinator _routeHeavyweightBlock:] + 360
25 CoreData 0x000000010cda3c02 -[NSPersistentStoreCoordinator executeRequest:withContext:error:] + 642
26 CoreData 0x000000010cdcc69b -[NSManagedObjectContext save:] + 1579
其他信息:
我不知道这是否相关,但这个对象数组是核心数据实体的可转换属性。
问题:
我怎样才能成功地将这个映射到 json?我曾尝试使用 BuyableUnit *unit;,但它从未初始化,因此调试器指向 0x0(nil)。
解决方案
根据崩溃日志,我认为您需要NSCoding
在您的类中遵守协议BuyableUnit
并实现以下方法:
- (void)encodeWithCoder:(NSCoder *)aCoder {
[aCoder encodeObject:self.type forKey:@"type"];
[aCoder encodeObject:self.unit forKey:@"unit"];
}
- (id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super init]) {
self.type = [aDecoder decodeObjectForKey:@"type"];
self.unit = [aDecoder decodeObjectForKey:@"unit"];
}
return self;
}
推荐阅读
- c - 由于 unsigned int 变量类型在 c 中有无限循环时会发生什么
- python - 在Python中将垂直系列重塑为水平系列
- javascript - 引用静态方法内部的类而不使用其名称
- javascript - 将变量从 javascript 传递给 python,无需重新加载页面
- javascript - Non-greedy capturing parenthesis
- python-3.x - 如何为 CIFAR 10 pytorch 教程添加 GPU 计算?
- amazon-web-services - 如何通过节点红色 MQTT 设置 AWS SNS 电子邮件的电子邮件主题?
- swift - “找不到适用于应用程序的 WatchKit 1.0 应用程序包”Xcode 10.1 Swift 4.2
- excel - 如何使用 JCL 将 SQL 结果处理成 CSV/Excel 附件?
- javascript - 如何在图表中使用 CSV 中的值?