我想在Swift 2中实现一个简单的GKGameModel
。Apple的示例在Objective-C中表达,并包含此方法声明(根据协议NSCopying
的要求GKGameModel
继承):
- (id)copyWithZone:(NSZone *)zone {
AAPLBoard *copy = [[[self class] allocWithZone:zone] init];
[copy setGameModel:self];
return copy;
}
这如何转化为Swift 2?以下是否适用于效率和忽略区域?
func copyWithZone(zone: NSZone) -> AnyObject {
let copy = GameModel()
// ... copy properties
return copy
}
答案 0 :(得分:35)
NSZone
已不再在Objective-C中使用了很长时间。传递zone
参数被忽略。引自allocWithZone...
文档:
此方法存在历史原因;内存区域不再存在 由Objective-C使用。
你也可以安全地忽略它。
以下是如何符合NSCopying
协议的示例。
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。此示例适用于Xcode版本7.0 beta 5(7A176x)。特别是dynamicType.init(self)
。
编辑Swift 3
以下是不推荐使用dynamicType
的Swift 3的copyWithZone方法实现:
func copy(with zone: NSZone? = nil) -> Any
{
return type(of:self).init(self)
}
答案 1 :(得分:1)
Swift4,Helium的PlayItem对象:
// 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)
}
答案 2 :(得分:0)
A COPY OF ALREADY PRESENT array 字典的最后一个元素被复制并使用值创建。
在 Xcode 12.1 swift 5 中工作
// 模型类,其中为类定义了模型
import Foundation
class SampleClass: NSObject, NSCopying, Codable {
var variable1: String?
var variable2: String?
var variable3: String?
required override init() {
}
required init(_ model: SampleClass) {
self.variable1 = model.variable1
self.variable2 = model.variable2
self.variable3 = model.variable3
}
func copy(with zone: NSZone? = nil) -> Any
{
return type(of:self).init(self)
}
init(_ dictionary: [String: Any]) {
self.variable1 = dictionary["variable1"] as? String
self.variable2 = dictionary["variable2"] as? String
self.variable3 = dictionary["variable3"] as? String
}
}
// How to use it in ViewController in a button where data from the model is already in a array of dictionary
@IBAction func duplicate(_ sender: Any) {
if self.currentArray > 0 {
if let content = self.self.currentArray.last {
let anotherCopy = content.copy() as! SampleClass
self.self.currentArray.append(anotherCopy)
}
}
}