我在Swift操场上有以下示例,试图在Swift中实现一个副本构造函数:
class Shape : NSObject {
var color : String
override init() {
color = "Red"
}
init(copyFrom: Shape) {
color = copyFrom.color
}
}
class Square : Shape {
var length : Double
override init() {
super.init()
length = 10.0
}
init(copyFrom: Square) { /* Compilation error here! */
super.init(copyFrom: copyFrom)
length = copyFrom.length
}
}
let s : Square = Square() // {{color "Red"} length 10.0}
let copy = Square(copyFrom: s) // {{color "Red"} length 10.0}
s.color = "Blue" // {{color "Blue"} length 10.0}
s // {{color "Blue"} length 10.0}
copy // {{color "Red"} length 10.0}
问题在于这实际上并不能以当前形式进行编译。在子类中的init(copyFrom: Square)
方法上Square
,报告此错误:
Overriding method with selector 'initWithCopyFrom:' has incompatible type '(Square) -> Square'
如果它不是一个构造函数,就好像是一个常规对象,这个问题就很有意义了,您可以传递一个超类中期望的类型,但是在子类中已被重写以限制更严格:func
let mySquare : Shape = Square() // Note the var is a SHAPE
mySquare.someShapeMethod("Test") // If Square overrides someShapeMethod() to expect Int, compiler errors out to protect us here.
但是事实是它是构造函数,这使我相信我应该能够覆盖它并提供不同的方法签名,因为在编译时绝对知道对象的类型是什么。
如果我更改Shape
为不再扩展,此问题将消失NSObject
。但是,由于包含在现有的Objective-C代码中,因此需要对其进行扩展NSObject
。
如何更新我的复制构造函数,以允许aShape
知道从a复制Shape
,而允许aSquare
知道从a复制Square
?
init(copyFrom: Square)
是的重载,而不是覆盖init(copyFrom: Shape)
。我的意思是,它们是不相关的方法,因为它们接受不同的类型。在Swift中这是可以接受的。在ObjC中,这是非法的。ObjC中没有重载。
Swift初始化器不会自动继承。因此,在Swift中,您无法尝试将random复制Shape
为Square
。初始化程序不可用。但是在ObjC中,初始化器会自动继承(您不能阻止它们这样做)。因此,如果您有一个方法initWithCopyFrom:(*Shape)
,则要求每个子类都愿意接受它。这意味着您可以(在ObjC中)尝试创建一个Circle作为Square的副本。那当然是胡说八道。
如果这是NSObject
子类,则应使用NSCopying
。这是您的处理方式:
import Foundation
class Shape : NSObject, NSCopying { // <== Note NSCopying
var color : String
required override init() { // <== Need "required" because we need to call dynamicType() below
color = "Red"
}
func copyWithZone(zone: NSZone) -> AnyObject { // <== NSCopying
// *** Construct "one of my current class". This is why init() is a required initializer
let theCopy = self.dynamicType()
theCopy.color = self.color
return theCopy
}
}
class Square : Shape {
var length : Double
required init() {
length = 10.0
super.init()
}
override func copyWithZone(zone: NSZone) -> AnyObject { // <== NSCopying
let theCopy = super.copyWithZone(zone) as Square // <== Need casting since it returns AnyObject
theCopy.length = self.length
return theCopy
}
}
let s = Square() // {{color "Red"} length 10.0}
let copy = s.copy() as Square // {{color "Red"} length 10.0} // <== copy() requires a cast
s.color = "Blue" // {{color "Blue"} length 10.0}
s // {{color "Blue"} length 10.0}
copy // {{color "Red"}
迅捷3
class Shape: NSObject, NSCopying {
required override init() {
super.init()
}
func copy(with zone: NSZone? = nil) -> Any {
let copy = type(of: self).init()
return copy
}
}
class Square: Shape {
required override init() {
super.init()
}
func copy(with zone: NSZone? = nil) -> Any {
let copy = super.copy(with: zone) as! Square
copy.foo = self.foo
......
return copy
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句