Swift小记一

1、输出地址

print(String(format: "%p", "temp"))

2、判断字符串是否为空串、是否为nil

为String添加一个分类

extension String {
    /// 字符串是否为nil或是否为空串
    ///
    /// - Parameter str: 指定字符串
    /// - Returns: true,字符串为nil或空串;false字符串有字符长度
    static func isEmptyString(_ str: String?) -> Bool {
        // 判断字符串是否被创建
        if let temp = str {
            // isEmpty是判断字符串是否为空串,使用之前要保证字符串已被创建,不可以为nil
            if temp.isEmpty == true {
                return true
            } else {
                return false
            }
        } else {
            return true
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/xiu619544553/p/9051758.html