My question is simple: what is the best way to conform to the NSMutableCopying
protocol on my own Swift class (possibly with some code explanation)?
class CustomClass: NSObject, NSMutableCopying {
var string1: String
var string2: String
required override init() {
self.string1 = "string1"
self.string2 = "string2"
}
// MARK: NSMutableCopying ??
}
In Objc, I use:
+ (instancetype)allocWithZone:(struct _NSZone *)zone OBJC_SWIFT_UNAVAILABLE("use object initializers instead");
but this method unavailable in Swift.
So I implement like this:
class CustomClass: NSObject, NSMutableCopying {
var string1: String
var string2: String
required override init() {
self.string1 = "string1"
self.string2 = "string2"
}
func mutableCopyWithZone(zone: NSZone) -> AnyObject {
let customObject = CustomClass()
customObject.string1 = string1
customObject.string2 = string2
return customObject
}
}
i'm not sure whether right or not, but i can't think of a better way.
Continuous attention..
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.