Swift-NSCopying 发表于 2017-02-10 | 自定义类想要支持拷贝,必须遵循 NSCopying 协议: 1234567891011121314151617181920212223class Student: NSCopying { var name: String // required 只能修饰类初始化方法,当子类初始化方法与父类的不同,子类必须实现初始化方法,并且要使用 required 而不是 overide required init(name: String) { self.name = name } func copy(with zone: NSZone? = nil) -> Any { let student = Student(name: self.name) return student }}let stu1 = Student(name: "JY")let stu2 = stu1.copy() as! Student// falseObjectIdentifier(stu1) == ObjectIdentifier(stu2)stu2.name = "YU"// JYstu1.name// YUstu2.name