Obj-C NSMutableArray 返回 nil

Obj-C NSMutableArray returning 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)。

根据崩溃日志,我认为您需要在 BuyableUnit class 中遵守 NSCoding 协议并实现以下方法:

- (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;
}