程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何在Swift子类中实现复制构造函数?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

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

开发过程中遇到如何在Swift子类中实现复制构造函数?的问题如何解决?下面主要结合日常开发的经验,给出你关于如何在Swift子类中实现复制构造函数?的解决方法建议,希望对你解决如何在Swift子类中实现复制构造函数?有所启发或帮助;

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"}

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
    }

}

解决方法

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

大佬总结

以上是大佬教程为你收集整理的如何在Swift子类中实现复制构造函数?全部内容,希望文章能够帮你解决如何在Swift子类中实现复制构造函数?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: