How exactly does the “let” keyword work in Swift?

后端 未结 6 860
时光说笑
时光说笑 2020-12-02 23:27

I\'ve read this simple explanation in the guide:

The value of a constant doesn’t need to be known at compile time, but you must assign it a value exa

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-02 23:46

    Swift properties:

    Swift Properties official documentation

    In its simplest form, a stored property is a constant or variable that is stored as part of an instance of a particular class or structure. Stored properties can be either variable stored properties (introduced by the varkeyword) or constant stored properties (introduced by the let keyword).

    Example:

    The example below defines a structure called FixedLengthRange, which describes a range of integers whose range length cannot be changed once it is created:

    struct FixedLengthRange {
        var firstValue: Int
        let length: Int
    }
    

    Instances of FixedLengthRange have a variable stored property called firstValue and a constant stored property called length. In the example above, length is initialized when the new range is created and cannot be changed thereafter, because it is a constant property.

提交回复
热议问题