简体   繁体   English

无法在 swift 中使用另一个协议扩展协议

[英]Can not extend a protocol with another protocol in swift

Am trying to implement this piece of code in my project.我正在尝试在我的项目中实现这段代码。

private protocol AnyOptional {
    var isNil: Bool { get }
}

extension Optional: AnyOptional {
    var isNil: Bool { self == nil }
}

I saw it on SwiftBySundell https://www.swiftbysundell.com/articles/property-wrappers-in-swift/ .我在 SwiftBySundell https://www.swiftbysundell.com/articles/property-wrappers-in-swift/上看到了它。 But unfortunately am getting this error " Extension of protocol 'Optional' cannot have an inheritance clause ".但不幸的是,我收到了这个错误“协议‘可选’的扩展不能有继承条款”。 Although when I tried the same code on a playground it worked just fine.虽然当我在操场上尝试相同的代码时,它工作得很好。 Any idea why?知道为什么吗?

Check to make sure that the Optional type name isn't being overridden by a third-party module (or your own module even).检查以确保Optional类型名称没有被第三方模块(甚至您自己的模块)覆盖。 If it is, then you can use Swift.Optional instead to refer to the Optional enum built into Swift.如果是,那么您可以改用Swift.Optional来引用 Swift 中内置的Optional枚举。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM