Ios Swift编码中的EXC\u BAD\u指令 func encodeWithCoder(aCoder:NSCoder!){ aCoder.encodeObject(title.bridgeToObjectiveC(),forKey:“title”) aCoder.encodeObject(self.artist.bridgeToObjectiveC(),forKey:“艺术家”) } init(编码器aDecoder:NSCoder!){ NSLog(“标题:%@”,aDecoder.decodeObjectForKey(“标题”)作为NSString);//

Ios Swift编码中的EXC\u BAD\u指令 func encodeWithCoder(aCoder:NSCoder!){ aCoder.encodeObject(title.bridgeToObjectiveC(),forKey:“title”) aCoder.encodeObject(self.artist.bridgeToObjectiveC(),forKey:“艺术家”) } init(编码器aDecoder:NSCoder!){ NSLog(“标题:%@”,aDecoder.decodeObjectForKey(“标题”)作为NSString);//,ios,cocoa-touch,cocoa,swift,Ios,Cocoa Touch,Cocoa,Swift,仔细检查您是从NSObject继承的。如果您想做foundation-y的事情,您确实需要一个超类。不管怎样,这是我的问题。尝试在不使用桥接调用的情况下执行此操作。它仍然可以无缝桥接到NSString。也许您可以尝试此操作 func encodeWithCoder(aCoder: NSCoder!){ aCoder.encodeObject(title.bridgeToObjectiveC(), forKey: "title") aCoder.encode

仔细检查您是从NSObject继承的。如果您想做foundation-y的事情,您确实需要一个超类。不管怎样,这是我的问题。

尝试在不使用桥接调用的情况下执行此操作。它仍然可以无缝桥接到NSString。

也许您可以尝试此操作

    func encodeWithCoder(aCoder: NSCoder!){
        aCoder.encodeObject(title.bridgeToObjectiveC(), forKey: "title")
        aCoder.encodeObject(self.artist.bridgeToObjectiveC(), forKey: "artist")
    }

    init(coder aDecoder: NSCoder!) {
        NSLog("title: %@", aDecoder.decodeObjectForKey("title") as NSString); //<---|Causes crash here but still logs the title
        self.title = String.bridgeFromObjectiveC(aDecoder.decodeObjectForKey("title") as NSString)
    }

下面是我在阅读了位于or的文档后构建的一个工作示例(作为命令行实用程序)。注意:我从Objective-C转换了它,因为这是我链接到的PDF中的内容

正如上述条例草案所述,我们毋须进行桥接

import Foundation

class AnyClass:NSObject, NSCoding{

    var title : String
    var artist : String

    init() {
        self.title = "TitleName"
        self.artist = "ArtistName"
    }

    init(coder aDecoder: NSCoder!) {        
        title = aDecoder.decodeObjectForKey("title") as String
        artist = aDecoder.decodeObjectForKey("artist") as String

        println(title)
    }


    func encodeWithCoder(_aCoder: NSCoder!){
        _aCoder.encodeObject(self.title, forKey: "title")
        _aCoder.encodeObject(self.artist, forKey: "artist")

    }
}

你为什么要使用
NSLog
而不是
println
?你应该在Swift中使用
println
作为你的日志记录语句,没有真正的理由使用
NSLog
@lx。很抱歉,这是一种习惯。在我的代码中替换它并不能解决问题though@ixt使用
NSLog
而不是
确实是有原因的>println
,NSLog为消息添加时间戳。也许您的密钥也需要连接到objective c?很遗憾,我们没有真正的swift框架、库或模式。感谢您的回答,但它已经
类PlayerItem:NSObject,NSCoding{
是的,当我用String而不是NSString进行此尝试时,它实际上再次调用了EXC错误访问。
import Foundation

class Gloppo2 : NSObject, NSCoding {
    var Dorf2:String = ""
    var Druben2:String = ""

    func encodeWithCoder(aCoder: NSCoder!) {
        aCoder.encodeObject(self.Dorf2, forKey: "Dorf2")
        aCoder.encodeObject(self.Druben2, forKey: "Druben2")
    }

    init(coder aDecoder: NSCoder!) {
        Dorf2 = aDecoder.decodeObjectForKey("Dorf2") as NSString!
        Druben2 = aDecoder.decodeObjectForKey("Druben2") as NSString!
    }

    init() {

    }
}

var newGloppo = Gloppo2()
newGloppo.Dorf2 = "Sue"
newGloppo.Druben2 = "Perman"

NSKeyedArchiver.archiveRootObject(newGloppo, toFile: "gloppo2.bin")

var newGloppo2 = NSKeyedUnarchiver.unarchiveObjectWithFile("gloppo2.bin") as Gloppo2

println(newGloppo2.Dorf2)
println(newGloppo2.Druben2)