20

我想GKGameModel在 Swift 2 中实现一个简单的。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
}
4

3 回答 3

41

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

PS 此示例适用于 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)
}
于 2015-08-20T08:44:55.800 回答
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)
}
于 2019-08-24T12:42:43.307 回答
0

复制字典的最后一个元素并使用值创建一个已存在数组的副本。

在 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)
            }
        }
    }
于 2021-05-14T13:44:28.807 回答