How to add an optional string extension?

后端 未结 9 595
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-13 23:44

You can create a String extension like so:

extension String {
   func someFunc -> Bool { ... }
}

but what if you want it to apply to opt

9条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-14 00:14

    You can create an optional string extension. I did the following to set an optional string to empty if it was nil :

    extension Optional where Wrapped == String {
    
        mutating func setToEmptyIfNil() {
            guard self != nil else {
                self = ""
                return
            }
        }
    
    }
    

提交回复
热议问题