swift - iOS swift : does not save pList with class

标签 swift class plist

我尝试将我自己的类“Solutions”保存在 pList 中。它包含另一个类“So​​lution”的数组。

let dict: NSMutableDictionary = ["XInitializerItem": Solution() ]
// saving values
dict.setObject(solutions, forKey: "best")

// writing back
dict.writeToFile(path, atomically: false)

但文件未写入。我认为这取决于类(class)。对于 String 或 Int 这样的简单数据类型是可行的。缺少什么?

我添加一些代码片段:

func savePlist( solutions : [Solution]) {

    let path = getDocPath(plistfile + ".plist")

    let dict: NSMutableDictionary = ["XInitializerItem": Solution() ]
    // saving values
    dict.setObject(solutions, forKey: "best")

    // writing back
    dict.writeToFile(path, atomically: false)

}

解决方案定义为:

class Solution : NSObject, NSCoding {   // struct
    let word     : String
    let score    : Int
    let priority : Int      // needed for sorting criteria
    let turn     : Int      // when was this reached?
    let date     : NSDate   // same
    let width    : Int      // which dimenions of the game?
    let height   : Int
    var new      : Bool     // in this session (or loaded from best list)

    init(word : String = "", score : Int = 0, bonus : Int = 0, turn : Int = 0, new : Bool = true) {
        self.word = word
        self.score = score
        self.priority = score + bonus
        self.turn = turn
        self.date = NSDate()
        self.width = NumColumns
        self.height  = NumRows
        self.new  = new
    }

    required init(coder aDecoder: NSCoder) {
        word     = aDecoder.decodeObjectForKey("word")     as! String
        score    = aDecoder.decodeObjectForKey("score")    as! Int
        priority = aDecoder.decodeObjectForKey("priority") as! Int
        turn     = aDecoder.decodeObjectForKey("turn")     as! Int
        date     = aDecoder.decodeObjectForKey("date")     as! NSDate
        width    = aDecoder.decodeObjectForKey("width")    as! Int
        height   = aDecoder.decodeObjectForKey("height")   as! Int
        new      = false
    }

    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(word, forKey: "word")
        aCoder.encodeObject(score, forKey: "score")
    }
}

class Solutions : NSObject, NSCoding {
    var solutions = [Solution]()   // for high score etc...

    override init() {
    }

    func bestSolution() -> Solution? {
        if solutions.count == 0 { return nil }
        var best = solutions[0]
        for s in solutions {
            if s.score > best.score {
                best = s
            }
        }
        return best
        //           return Solutions.reduce(0, { max($0.score, $1.score) })
    }

    func longestSolution() -> Solution? {
        if solutions.count == 0 { return nil }
        var best = solutions[0]
        for s in solutions {
            if s.word.characters.count > best.word.characters.count {
                best = s
            }
        }
        return best
        //           return Solutions.reduce(0, { max($0.score, $1.score) })
    }

    required init(coder aDecoder: NSCoder) {
        solutions = aDecoder.decodeObjectForKey("solutions") as! [Solution]
    }

    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(solutions, forKey: "solutions")
    }
}

最佳答案

为了使用用户生成的类编写 plist,该类需要支持 NSCoding 协议(protocol)。

来自Introduction to Property Lists :

But if objects conform to the NSCoding protocol they can be archived to NSData objects, which are property list–compatible objects.

关于swift - iOS swift : does not save pList with class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32317164/

相关文章:

swift - 放置 UIActivityIndi​​catorView

iphone - 为什么在 iOS 9.3 中杀死/终止应用程序后后台位置不起作用

ios - 使用推送通知配置文件安装应用程序失败

java - Class.forName() 是什么意思

objective-c - 从 plist 文件写入和读取

ios - UILabel 中的可点击用户名和主题标签(不是 UIWebView!)

C++(类和指针)

java - 访问类字节?

PHP:PLIST/XML 与 MySQL 的性能对比?

ios - 在 iOS 上对用户隐藏注册码