将自定义数组保存到Core Data中

时间:2017-06-15 08:20:21

标签: arrays macos cocoa core-data swift3

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

// 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

1 个答案:

答案 0 :(得分:1)

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

请注意,符合NSCoding要求继承自NSObject,这意味着您无法在Swift结构中使用它。