美文网首页ios 开发之没事来看看
swift 如何优雅的判断String 是否为nil 或者空格

swift 如何优雅的判断String 是否为nil 或者空格

作者: 等这姑娘老在我心里 | 来源:发表于2019-05-27 15:03 被阅读0次
    extension String{
        
        /// check string cellection is whiteSpace
        var isBlank : Bool{
            return allSatisfy({$0.isWhitespace})
        }
    }
    
    
    extension Optional where Wrapped == String{
        var isBlank : Bool{
            return self?.isBlank ?? true
        }
    }
    
    
    var title: String? = nil
    title.isBlank            // true
    title = ""               
    title.isBlank            // true
    title = "  \t  "               
    title.isBlank            // true
    title = "Hello"
    title.isBlank            // false
    

    相关文章

      网友评论

        本文标题:swift 如何优雅的判断String 是否为nil 或者空格

        本文链接:https://www.haomeiwen.com/subject/ceshtctx.html