您可以像下面这样创建一个 String 扩展:
extension String {
func someFunc() -> Bool { return true }
}
但是如果你想把它应用到可选字符串上呢?
var optionalString: String? = ""
optionalString!.someFunc() /* String? does not have a member someFunc */
试图添加 extension String? {
会产生错误:
受约束的扩展必须在非专门的泛型类型“ Options”上声明,其约束由“ where”子句指定