随着介绍open
Swift 3.0 中的关键字 (Swift 中的“open”关键字是什么? https://stackoverflow.com/questions/38947101/what-is-the-open-keyword-in-swift).
注意:仅限于扩展NSObject
派生类或@objc
归因方法/属性。
声明和使用的代码public
(class
) 扩展中的方法/属性across模块/框架损坏了,如public
不再意味着在定义模块之外“可重写”。
Example:
public extension UIManagedDocument {
public class func primaryDocumentName() -> String {
return "Document"
}
public class func primaryStoreURL() -> URL {
let documentsURL = FileManager.default.userDocumentsURL
return URL(fileURLWithPath: self.primaryDocumentName(), isDirectory: false, relativeTo: documentsURL)
}
public class func primaryModelName() -> String? {
return "Model"
}
}
- 原提案(SE-0117 https://github.com/apple/swift-evolution/blob/master/proposals/0117-non-public-subclassable-by-default.md)专注于子类化,没有提及扩展。
- 目前扩展不支持
open
关键字(你不能写open extension NSObject
也open func Method()
)
Question:是否有解决方法可以覆盖扩展提供的方法/属性across模块/框架?
除非我弄错了,否则你can将扩展方法声明为open
在你的框架中,如果你只是省略public
关键词
在扩展声明中:
extension UIManagedDocument {
open class func primaryDocumentName() -> String {
return "Document"
}
// ...
}
然后(对于NSObject
子类或@objc
成员)你可以重写该方法
在主应用程序(或任何模块)的自定义子类中:
class MyManagedDocument: UIManagedDocument {
override class func primaryDocumentName() -> String {
return "MyDocument"
}
// ...
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)