一尘不染

如何在Swift子类中实现复制构造函数?

swift

我在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

如何更新我的复制构造函数,以允许a Shape知道从a复制Shape,而允许a Square知道从a复制Square


阅读 217

收藏
2020-07-07

共1个答案

一尘不染

init(copyFrom: Square)是的重载,而不是覆盖init(copyFrom: Shape)。我的意思是,它们是不相关的方法,因为它们接受不同的类型。在Swift中这是可以接受的。在ObjC中,这是非法的。ObjC中没有重载。

Swift初始化器不会自动继承。因此,在Swift中,您不能尝试将random复制ShapeSquare。初始化器不可用。但是在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
    }

}
2020-07-07