Let's say I have an object Person. (I know that Person has syntax errors. Please ignore those.)
interface StringList extends List<String> {}
Class<? extends StringList> c; OK!
My question is: Is it possible to have two separate encodeWithCoder methods..one that includes salary in the archived object, and one that does not. Something like:
class Person: NSObject, NSCoding {
var name : String
var salary: NSNumber
// MARK: NSCoding
required convenience init(coder decoder: NSCoder) {
self.init()
self.name = decoder.decodeObjectForKey("name") as String
self.salary = decoder.decodeObjectForKey("salary") as NSNumber
}
func encodeWithCoder(coder: NSCoder) {
coder.encodeObject(self.name, forKey: "name")
coder.encodeObject(self.salary, forKey: "salary")
}
}
What's the best way to go about implementing multiple archive methods in an NSObject, as a way of limiting the fields written to the archive?
答案 0 :(得分:0)
Rather than trying to have 2 versions of it you should figure out what it is that determines which encoding you want to use, and make that a separate flag/field and encode that too.
ord
Then you can do the same thing in func encodeWithCoder(coder: NSCoder) {
coder.encodeObject(self.name, forKey: "name")
if self.someFlag == true {
coder.encodeObject(self.salary, forKey: "salary")
}
// encode flag as well
}
by parsing the flag first and decoding the rest of the fields based on its value.