i want to write a parent class with copy function so all it's child could simply use copy() to copy object without implement copyWithZone , is that possible?
the NSCoding part is working fine , other class inherit from my SerializeModel could using archivedData and unarchiveObject without implement any code , so i want to do the same thing to my copy function
here is my class
class SerializeModel: NSObject, NSCoding{
override init() {
super.init()
}
required init(coder decoder: NSCoder) {
super.init()
let properties = propertyNames();
for var propertyName in properties {
self.setValue(decoder.decodeObject(forKey: propertyName) , forKey: propertyName)
}
}
func encode(with coder: NSCoder) {
let properties = propertyNames();
for var propertyName in properties {
coder.encode(self.value(forKey: propertyName), forKey: propertyName)
}
}
func propertyNames() -> [String] {
return Mirror(reflecting: self).children.flatMap() {$0.label}
}
// i want to make this function work
override func copy() -> Any {
let type = type(of: self)
let instance = type();
let properties = propertyNames();
for var propertyName in properties {
instance.setValue(self.value(forKey: propertyName) , forKey: propertyName)
}
return instance
}
}
this is one of it's child
class Location: SerializeModel {
var lastUpdateTime:String? = nil
var lantitude:Double = 0
var longitude:Double = 0
var locationProviderType:String = LocationProviderType.PROVIDER_NONE.rawValue
var isSended:Bool = false
}
i could use it to archivedData/unarchiveObject without other code.
Aucun commentaire:
Enregistrer un commentaire