问题
I'm trying a basic implementation of the NSCoding
protocol in swift, but it seems I can't success to unarchive an object after it has been correctly archived.
Here's my attempt
import Cocoa
class User: NSObject, NSCoding {
var name: String
init(name: String) {
self.name = name
}
init(coder aDecoder: NSCoder!) {
self.name = aDecoder.decodeObjectForKey("name") as String
}
func encodeWithCoder(aCoder: NSCoder!) {
aCoder.encodeObject(name, forKey: "name")
}
}
let user = User(name: "Gabriele")
let encodedUser = NSKeyedArchiver.archivedDataWithRootObject(user)
let decodedUser = NSKeyedUnarchiver.unarchiveObjectWithData(encodedUser) as User
Running this in the playground, it launches an exception on the last line. Here's the details
Execution was interrupted, reason: signal SIGABRT.
The process has been left at the point where it was interrupted, use "thread return -x" to return to the state before expression evaluation.
* thread #1: tid = 0x433bc, 0x00007fff9325e37a libsystem_kernel.dylib`__pthread_kill + 10, queue = 'com.apple.main-thread', stop reason = signal SIGABRT
* frame #0: 0x00007fff9325e37a libsystem_kernel.dylib`__pthread_kill + 10
frame #1: 0x00007fff8c3618f7 libsystem_pthread.dylib`pthread_kill + 90
frame #2: 0x00007fff935b462b libsystem_c.dylib`abort + 129
frame #3: 0x00007fff8813fa21 libc++abi.dylib`abort_message + 257
frame #4: 0x00007fff881679d1 libc++abi.dylib`default_terminate_handler() + 267
frame #5: 0x00007fff8538050d libobjc.A.dylib`_objc_terminate() + 103
frame #6: 0x00007fff881650a1 libc++abi.dylib`std::__terminate(void (*)()) + 8
frame #7: 0x00007fff88164b30 libc++abi.dylib`__cxa_throw + 121
frame #8: 0x00007fff8537c6a7 libobjc.A.dylib`objc_exception_throw + 341
frame #9: 0x00007fff8ec1962d CoreFoundation`+[NSException raise:format:] + 205
frame #10: 0x00007fff90dd9382 Foundation`_decodeObjectBinary + 2682
frame #11: 0x00007fff90dd8796 Foundation`_decodeObject + 278
frame #12: 0x00007fff90dfe159 Foundation`+[NSKeyedUnarchiver unarchiveObjectWithData:] + 89
The encoding works fine, as encodedUser
is a valid instance of NSData
(NSConcreteMutableData
, to be precise).
Is this some sort of interoperability bug of Cocoa API in swift or am I implementing the NSCoding
protocol wrong?
回答1:
As Martin pointed out the in the comments, the code works fine in a compiled app, so it's definitely a playground bug.
David, in the comments, suggested to use @objc(User)
in order to get a non-mangled class name, but it didn't help in this specific case, so - as of beta 2 - I still haven't find a workaround to make it work in the playground.
来源:https://stackoverflow.com/questions/24589933/nskeyedunarchiver-fails-to-decode-a-custom-object-in-swift