swift 类静态常量的类型推断

65 阅读1分钟

1,对于函数的调用,类型推断.

2,开发者自定义的静态常量,编译器无法做出这样的假设,所以要求必须提供初始值,以确保程序的正确性和可预测性。

public struct Name {
    var firstName: String = "John"
    var lastName: String = "Doe"

    public static let title: Name = Name()


    
}



func f (_ a: Name) {
    print(a)
}

// 这里不需要写成 f(Name.title), Name就自动推断了
f(.title)

SwiftUI 中的static的常量就没有提供默认值, 苹果的开发团队在设计 SwiftUI 框架时,已经在框架内部的初始化代码中为这些常量赋予了合适的初始值。这些初始化过程是在框架加载阶段完成的,对于开发者来说是不可见的。

extension Font {

    /// A font with the large title text style.
    public static let largeTitle: Font

}