美文网首页
Swift 5.6 新特性

Swift 5.6 新特性

作者: yyggzc521 | 来源:发表于2022-04-17 11:27 被阅读0次

    #unavailable

    Swift 5.6 前 #available 表示可用,Swift 5.6 之后增加了#unavailable表示不可用,二者意思相反。

    if #unavailable(iOS 15) {
        // iOS15不可用,即iOS15之前的代码可以正常工作
    } else {
        // iOS15的代码可以正常工作
    }
    

    注意:使用上与#available最大的区别是#unavailable不能使用平台通配符*

    类型占位符_

    使用 _ , _? 占用类型的位置,然后编译器通过类型推断可以推断出_ , _? 的类型

    // _?代替Double?
    var a: _? = 3.14
    a = nil
    
    // 数组的元素为Int类型,_代替Int
    let array: Array<_> = [1, 2, 3, 4, 6]
    
    // 字典的value为UIColor类型,_代替UIColor
    let colors: [String: _] = ["red": UIColor.red, "green": UIColor.green, "blue": UIColor.blue]
    

    CodingKeyRepresentable

    Swift 5.6 之前,如果字典的 Key 为非 Int 或 String 类型,通过 Codable 编码后得不到预期的结果

    5.6之前 的打印结果是这样的:["name","zhangsan","age","20","sex","male"]

    enum Student: String, Codable {
        case name, age, sex
    }
    
    // 字典
    let dict: [Student: String] = [.name: "zhangsan", .age: "20", .sex: "male"]
    // 编码
    let encoder = JSONEncoder()
    do {
        let student = try encoder.encode(dict)
        print(String(decoding: student, as: UTF8.self)) 
    } catch {
        fatalError(error.localizedDescription)
    }
    

    5.6之后增加了 CodingKeyRepresentable, 打印结果是这样的:{"sex":"male","name":"zhangsan","age":"20"}

    // 实现CodingKeyRepresentable协议
    enum Student: String, Codable, CodingKeyRepresentable {
        case name, age, sex
    }
    
    // 字典
    let dict: [Student: String] = [.name: "zhangsan", .age: "20", .sex: "male"]
    // 编码
    let encoder = JSONEncoder()
    do {
        let student = try encoder.encode(dict)
        print(String(decoding: student, as: UTF8.self))
    } catch {
        fatalError(error.localizedDescription)
    }
    

    存在类型 - any

    相关有助于理解的资料:https://juejin.cn/post/7078192732635676680
    Swift 5.6 之前协议的使用

    protocol SomeProtocol {
        func work()
    }
    
    class Student: SomeProtocol {
        func work() {
            print("Study")
        }
    }
    
    class Teacher: SomeProtocol {
        func work() {
            print("Teach")
        }
    }
    
    // 泛型函数,泛型遵守了协议
    func generic<T>(who: T) where T: SomeProtocol {
        who.work()
    }
    
    // 正确
    generic(who: Student())
    generic(who: Teacher())
    
    // 报错:Protocol 'SomeProtocol' as a type cannot conform to the protocol itself
    let student: SomeProtocol = Student()
    let teacher: SomeProtocol = Teacher()
    generic(who: student)
    generic(who: teacher)
    

    Swift 5.6 之后增加了一种新的类型—存在类型,表示为any 类型。改造上面函数并将初始化部分的SomeProtocol更改为存在类型any SomeProtocol,报错的代码变为正确

    protocol SomeProtocol {
        func work()
    }
    
    class Student: SomeProtocol {
        func work() {
            print("Study")
        }
    }
    
    class Teacher: SomeProtocol {
        func work() {
            print("Teach")
        }
    }
    
    // 泛型函数改为any存在类型函数
    func existential(who: any SomeProtocol) {
        who.work()
    }
    
    // 正确
    existential(who: Student())
    existential(who: Teacher())
    
    // 正确
    let student: any SomeProtocol = Student()
    let teacher: any SomeProtocol = Teacher()
    existential(who: student)
    existential(who: teacher)
    

    参考文章:
    https://juejin.cn/post/7077369199626027039

    相关文章

      网友评论

          本文标题:Swift 5.6 新特性

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