import UIKit
class User: NSObject,NSCopying,NSCoding {
var name = ""
var age = 0
required override init() {
}
required init?(coder aDecoder: NSCoder) {
super.init();
var count:UInt32 = 0;
let list = class_copyPropertyList(User.self, &count)
for i:Int in 0..<Int(count) {
let property = list![i]
let key = String.init(cString: property_getName(property))
self .setValue(aDecoder.decodeObject(forKey: key), forKey: key)
}
}
func encode(with aCoder: NSCoder) {
var count:UInt32 = 0;
let list = class_copyPropertyList(User.self, &count)
for i:Int in 0..<Int(count) {
let property = list![i]
let key = String.init(cString: property_getName(property))
aCoder .decodeObject(forKey: key)
}
}
func copy(with zone: NSZone? = nil) -> Any {
let copyObj = self
return copyObj
}
override func mutableCopy() -> Any {
let copyObj = type(of: self).init()
var count:UInt32 = 0
let list:UnsafeMutablePointer = class_copyPropertyList(User.self, &count)!
for i:Int in 0..<Int(count) {
let property:objc_property_t = list[i]!
let key = String(cString: ivar_getName(property))
let obj = self .value(forKey: key)
copyObj.setValue(obj, forKey: key)
}
return copyObj
}
}
我写了个swift 3 的 ,不知道有没有问题,有问题可以帮我指出来,谢谢