将自定义数组保存到 Core Data

Posted

技术标签:

【中文标题】将自定义数组保存到 Core Data【英文标题】:save custom array into Core Data 【发布时间】:2017-06-15 08:20:20 【问题描述】:

我有这段代码可以将一个简单的字符串数组保存到核心数据中并将其转换回数组:

// Convert it to binary data
let array = ["Haus", "Tier","Tür"]
let x = NSKeyedArchiver.archivedData(withRootObject: positionen)

// Convert it to an array
let data = coreData.value(forKey: "arrayPos") as! NSData
let unarchiveObject = NSKeyedUnarchiver.unarchiveObject(with: data as Data)
let arrayObject = unarchiveObject as! [String]

但是如果想像这样保存一个数组:

struct ArrayData 
    var positionsname:String
    var anzahl:Double
    var einheit:String
    var einzelpreis:Double
    var typ:String
    var katalog:Bool

var array = [ArrayData ]()

我的应用会崩溃。

2017-06-15 09:38:48.212788+0200 Programm[9364:608571] -[_SwiftValue encodeWithCoder:]: unrecognized selector sent to instance 0x6080000f4700
2017-06-15 09:38:48.214663+0200 Programm[9364:608571] [General] -[_SwiftValue encodeWithCoder:]: unrecognized selector sent to instance 0x6080000f4700
2017-06-15 09:38:48.285393+0200 Programm[9364:608571] [General] (
    0   CoreFoundation                      0x00007fffb49ac57b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x00007fffc9bf01da objc_exception_throw + 48
    2   CoreFoundation                      0x00007fffb4a2cf14 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
    3   CoreFoundation                      0x00007fffb491fc93 ___forwarding___ + 1059
    4   CoreFoundation                      0x00007fffb491f7e8 _CF_forwarding_prep_0 + 120
    5   Foundation                          0x00007fffb63ac95a _encodeObject + 1241
    6   Foundation                          0x00007fffb63adf0c -[NSKeyedArchiver _encodeArrayOfObjects:forKey:] + 460
    7   Foundation                          0x00007fffb63ac95a _encodeObject + 1241
    8   Foundation                          0x00007fffb63e8492 +[NSKeyedArchiver archivedDataWithRootObject:] + 156
    9   Programm                            0x000000010002177b _TFC8Programm18DokumentController12saveCoreDatafT_T_ + 907
    10  Programm                            0x0000000100020ef9 _TFC8Programm18DokumentController11getObjectIDfCSo14NSNotificationT_ + 377
    11  Programm                            0x00000001000213da _TToFC8Programm18DokumentController11getObjectIDfCSo14NSNotificationT_ + 58
    12  CoreFoundation                      0x00007fffb493954c __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 12
    13  CoreFoundation                      0x00007fffb493944b _CFXRegistrationPost + 427
    14  CoreFoundation                      0x00007fffb49391b2 ___CFXNotificationPost_block_invoke + 50
    15  CoreFoundation                      0x00007fffb48f7782 -[_CFXNotificationRegistrar find:object:observer:enumerator:] + 2018
    16  CoreFoundation                      0x00007fffb48f676b _CFXNotificationPost + 667
    17  Foundation                          0x00007fffb6338907 -[NSNotificationCenter postNotificationName:object:userInfo:] + 66
    18  Programm                            0x0000000100003eae _TFC8Programm16KundenController20showSelectedDocumentfT_T_ + 2094
    19  Programm                            0x0000000100003458 _TFC8Programm16KundenController21outlineViewClickedRowfCSo13NSOutlineViewT_ + 5672
    20  Programm                            0x000000010000366a _TToFC8Programm16KundenController21outlineViewClickedRowfCSo13NSOutlineViewT_ + 58
    21  libsystem_trace.dylib               0x00007fffca7033a7 _os_activity_initiate_impl + 53
    22  AppKit                              0x00007fffb2b9a721 -[NSApplication(NSResponder) sendAction:to:from:] + 456
    23  AppKit                              0x00007fffb267ecc4 -[NSControl sendAction:to:] + 86
    24  AppKit                              0x00007fffb26f5d10 -[NSTableView _sendAction:to:row:column:] + 111
    25  AppKit                              0x00007fffb26f450f -[NSTableView mouseDown:] + 7439
    26  AppKit                              0x00007fffb26f25d0 -[NSOutlineView mouseDown:] + 74
    27  AppKit                              0x00007fffb2d1624f -[NSWindow(NSEventRouting) _handleMouseDownEvent:isDelayedEvent:] + 6341
    28  AppKit                              0x00007fffb2d12a6c -[NSWindow(NSEventRouting) _reallySendEvent:isDelayedEvent:] + 1942
    29  AppKit                              0x00007fffb2d11f0a -[NSWindow(NSEventRouting) sendEvent:] + 541
    30  AppKit                              0x00007fffb2b96681 -[NSApplication(NSEvent) sendEvent:] + 1145
    31  AppKit                              0x00007fffb2411427 -[NSApplication run] + 1002
    32  AppKit                              0x00007fffb23dbe0e NSApplicationMain + 1237
    33  Programm                            0x000000010001319d main + 13
    34  libdyld.dylib                       0x00007fffca4d1235 start + 1
    35  ???                                 0x0000000000000003 0x0 + 3
)
2017-06-15 09:38:48.286692+0200 Programm[9364:608571] *** -[NSKeyedArchiver dealloc]: warning: NSKeyedArchiver deallocated without having had -finishEncoding called on it.

谁能解释我,我做错了什么? 我使用 swift 3 for OS X

【问题讨论】:

与Core Data相关ArrayData应该是一个实体,成员是属性。 (如果有原生等价物,请不要在 Swift 中使用 NS... 类 - 这里是 NSData)。 这是在 Core Data 中处理记录的指定方式。 【参考方案1】:

如果使用NSKeyedArchiver.archivedData(withRootObject:),根对象必须符合NSCoding。对于数组,数组的每个成员都必须符合。您会收到此错误,因为 ArrayData 没有这样做。堆栈跟踪显示 NSCoding 方法 encodeWithCoder 被调用的东西没有实现该方法。

请注意,符合NSCoding 需要从NSObject 继承,这意味着您不能在Swift 结构中使用它。

【讨论】:

以上是关于将自定义数组保存到 Core Data的主要内容,如果未能解决你的问题,请参考以下文章

如何将自定义对象保存在包含更多自定义对象的数组中

Swift:在 Core Data 中存储自定义类的数组

Swift 4 如何将自定义对象数组转换为 JSON

在 Core Data 中保存一组自定义类

将对象数组保存到 Core Data

Core Data 自定义结构数组