0

i have this code to save an simple string array into core data und convert it back to an array:

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

But if would like save an array like this:

struct ArrayData {
    var positionsname:String
    var anzahl:Double
    var einheit:String
    var einzelpreis:Double
    var typ:String
    var katalog:Bool
}
var array = [ArrayData ]()

My app will crashes.

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.

can anyone explain me, what i do wrong? i working with swift 3 for OS X

2
  • 1
    Related to Core Data ArrayData is supposed to be an entity and the members are the attributes. (And don't use NS... classes in Swift if there is a native equivalent - here NSData). Commented Jun 15, 2017 at 8:31
  • 1
    That's the designated way to handle records in Core Data. Commented Jun 15, 2017 at 8:53

1 Answer 1

1

If you use NSKeyedArchiver.archivedData(withRootObject:), the root object must conform to NSCoding. With an array, every member of the array must conform. You get this error because ArrayData doesn't do that. The stack trace shows that the NSCoding method encodeWithCoder was called on something that doesn't implement that method.

Note that conforming to NSCoding requires inheriting from NSObject, which means that you can't use it in a Swift struct.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.