如何符合NSCopying并在Swift 2中实施copyWithZone?
我想在Swift 2中实现一个简单的如何符合NSCopying并在Swift 2中实施copyWithZone?,swift,swift2,gameplay-kit,Swift,Swift2,Gameplay Kit,我想在Swift 2中实现一个简单的GKGameModel。苹果公司的示例用Objective-C表示,并包括此方法声明(根据协议NSCopying的要求,从该协议GKGameModel继承): 这是如何翻译成Swift 2的?就效率和忽略区域而言,以下各项是否合适 func copyWithZone(zone: NSZone) -> AnyObject { let copy = GameModel() // ... copy properties return c
GKGameModel
。苹果公司的示例用Objective-C表示,并包括此方法声明(根据协议NSCopying
的要求,从该协议GKGameModel
继承):
这是如何翻译成Swift 2的?就效率和忽略区域而言,以下各项是否合适
func copyWithZone(zone: NSZone) -> AnyObject {
let copy = GameModel()
// ... copy properties
return copy
}
NSZone
在很长一段时间内不再在Objective-C中使用。并忽略传递的区域
参数。引用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
注意:本示例适用于Xcode版本7.0 beta 5(7A176x)。尤其是dynamicType.init(self)
为Swift 3编辑
以下是Swift 3的copyWithZone方法实现,因为dynamicType
已被弃用:
func copy(with zone: NSZone? = nil) -> Any
{
return type(of:self).init(self)
}
Swift4的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)
}
复制并创建字典中已存在的数组最后一个元素的副本 在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)
}
}
}
我试着为我的类实现它,但是在复制类变量的同时如何创建副本呢?现在,我得到一个新实例,它没有从旧实例复制属性。如何一次复制一个类的所有属性,而不是一个接一个地进行赋值。因为你看,我有一个大约有50个属性的类,我很可能会错过其中一些属性的赋值。有出路吗?
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)
}
}
}