您可以像这样创建一个String扩展:
extension String { func someFunc -> Bool { ... } }
但是如果你想将它应用于可选字符串呢?
var optionalString :String? = "" optionalString!.someFunc() /* String? does not have a member someFunc */
在Swift 3.1中,您还可以为可选值添加扩展名:
原文链接:/swift/319988.htmlextension Optional where Wrapped == String { var isBlank: Bool { return self?.isBlank ?? true } }