I would like to implement a simple GKGameModel
in Swift 2. Apple's example is expressed in Objective-C and includes this method declaration (as required by protocol NSCopying
from which GKGameModel
inherits):
- (id)copyWithZone:(NSZone *)zone {
AAPLBoard *copy = [[[self class] allocWithZone:zone] init];
[copy setGameModel:self];
return copy;
}
How does this translate into Swift 2? Is the following appropriate in terms of efficiency and ignoring zone?
func copyWithZone(zone: NSZone) -> AnyObject {
let copy = GameModel()
// ... copy properties
return copy
}
NSZone
is no longer used in Objective-C for a long time. And passed zone
argument is ignored. Quote from the allocWithZone...
docs:
This method exists for historical reasons; memory zones are no longer used by Objective-C.
You're safe to ignore it as well.
Here's an example how to conform to NSCopying
protocol.
class GameModel: NSObject, NSCopying {
var someProperty: Int = 0
required override init() {
// This initializer must be required, because another
// initializer `init(_ model: GameModel)` is required
// too and we would like to instantiate `GameModel`
// with simple `GameModel()` as well.
}
required init(_ model: GameModel) {
// This initializer must be required unless `GameModel`
// class is `final`
someProperty = model.someProperty
}
func copyWithZone(zone: NSZone) -> AnyObject {
// This is the reason why `init(_ model: GameModel)`
// must be required, because `GameModel` is not `final`.
return self.dynamicType.init(self)
}
}
let model = GameModel()
model.someProperty = 10
let modelCopy = GameModel(model)
modelCopy.someProperty = 20
let anotherModelCopy = modelCopy.copy() as! GameModel
anotherModelCopy.someProperty = 30
print(model.someProperty) // 10
print(modelCopy.someProperty) // 20
print(anotherModelCopy.someProperty) // 30
P.S. This example is for Xcode Version 7.0 beta 5 (7A176x). Especially the dynamicType.init(self)
.
Edit for Swift 3
Below is the copyWithZone method implementation for Swift 3 as dynamicType
has been deprecated:
func copy(with zone: NSZone? = nil) -> Any
{
return type(of:self).init(self)
}
Swift4, Helium's PlayItem object:
// MARK:- NSCopying
convenience required init(_ with: PlayItem) {
self.init()
self.name = with.name
self.link = with.link
self.date = with.date
self.time = with.time
self.rank = with.rank
self.rect = with.rect
self.plays = with.plays
self.label = with.label
self.hover = with.hover
self.alpha = with.alpha
self.trans = with.trans
self.agent = with.agent
self.tabby = with.tabby
}
func copy(with zone: NSZone? = nil) -> Any
{
return type(of:self).init(self)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With