What is the use of “static” keyword if “let” keyword used to define constants/immutables in swift?

后端 未结 6 500
无人共我
无人共我 2020-12-07 09:40

I\'m little bit confused about using static keyword in swift. As we know swift introduces let keyword to declare immutable objects. Like de

6条回答
  •  天涯浪人
    2020-12-07 10:31

    "The let keyword defines a constant" is confusing for beginners who are coming from C# background (like me). In C# terms, you can think of "let" as "readonly" variable.

    (answer to How exactly does the “let” keyword work in Swift?)

    Use both static and let to define constant

    public static let pi = 3.1416            // swift
    
    public const double pi = 3.1416;         // C#
    public static final double pi = 3.1416   // Java     
    

    Whenever I use let to define constant, it feels like I am using readonly of C#. So, I use both static and let to define constant in swift.

提交回复
热议问题