NSKeyedUnarchiver无法快速解码自定义对象 [英] NSKeyedUnarchiver fails to decode a custom object in swift

查看:220
本文介绍了NSKeyedUnarchiver无法快速解码自定义对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试快速实现NSCoding协议的基本实现,但是在正确归档对象之后,似乎无法成功取消归档该对象.

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.

这是我的尝试

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

编码工作正常,因为encodedUserNSData(准确地说是NSConcreteMutableData)的有效实例.

The encoding works fine, as encodedUser is a valid instance of NSData (NSConcreteMutableData, to be precise).

这是Cocoa API的某种互操作性错误,还是我实现NSCoding协议有误?

Is this some sort of interoperability bug of Cocoa API in swift or am I implementing the NSCoding protocol wrong?

推荐答案

正如Martin在注释中指出的那样,该代码在已编译的应用程序中可以正常工作,因此绝对是一个游乐场错误.

As Martin pointed out the in the comments, the code works fine in a compiled app, so it's definitely a playground bug.

David在评论中建议使用@objc(User)来获得一个没有恶意的类名,但是在这种特定情况下它无济于事,所以-从beta 2开始-我仍然找不到使它在操场上工作的解决方法.

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.

这篇关于NSKeyedUnarchiver无法快速解码自定义对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆