swift - Swift 4 中如何遵循 NSCopying 并实现 copyWithZone?

标签 swift swift4

当我尝试使用 swift 3 方式时,我无法遵守 NSCopying 协议(protocol), 我的代码是:

public func copy(with zone: NSZone? = nil) -> Any {
    return type(of:self).init(self)
}

但是出现错误: 无法调用非函数类型“ProductType”的值

这里有什么问题?

最佳答案

我刚刚找到了解决方案,将函数更改为:

public func copy(with zone: NSZone? = nil) -> Any {
     return Swift.type(of:self).init(self)
 }

关于swift - Swift 4 中如何遵循 NSCopying 并实现 copyWithZone?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46062667/

相关文章:

Swift 可失败初始化器调用 ExpressibleByStringLiteral 初始化器

swift - IOS Swift 如何 fatal error 展开可选值

ios - 重新加载数据时 UICollectionView 滚动性能降低

ios - -[_ SwiftValue integerValue]:无法识别的选择器发送到实例0x60000044d560使用Google Mobile Vision时出错

ios - swift ,相机图像不返回

javascript - 仅针对一项特定功能解析连接超时

ios - AutoLayout 根据 child 设置 UITableViewCell 的高度

sprite-kit - 影响子节点的父节点的 Swift 4 SpriteKit : How to prevent the . alpha 属性?

swift - 无法将类型 'NSImageInterpolation.Type' 的值转换为预期参数类型 '[NSImageRep.HintKey : Any]?'

ios - 如何在类方法中返回 self ?