How to archive and unarchive custom objects in Swift? Or how to save custom object to NSUserDefaults in Swift?

后端 未结 5 1255
梦毁少年i
梦毁少年i 2020-12-10 02:46

I have a class

class Player {

    var name = \"\"

    func encodeWithCoder(encoder: NSCoder) {
        encoder.encodeObject(name)
    }

    func initWith         


        
5条回答
  •  忘掉有多难
    2020-12-10 03:10

    NSKeyedArchiver will only work with Objective-C classes, not pure Swift classes. You can bridge your class to Objective-C by marking it with the @objc attribute or by inheriting from an Objective-C class such as NSObject.

    See Using Swift with Cocoa and Objective-C for more information.

提交回复
热议问题