You can create a String extension like so:
extension String {
func someFunc() -> Bool { return true }
}
but what if you want it to apply to optional string?
var optionalString: String? = ""
optionalString!.someFunc() /* String? does not have a member someFunc */
Attempting to add extension String? {
produces the error:
Constrained extension must be declared on the unspecialized generic type 'Optional' with constraints specified by a 'where' clause
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…