Why can the structure declared by let be changed?

In the following code, 'ptr' is a structure declared by let, but its member variable 'pointee' can be changed, why? let ptr = UnsafeMutablePointer<Int>.allocate(capacity:1) ptr.pointee = 1

+4
source share
4 answers

UnsafeMutablePointer is a structure but announces

public struct UnsafeMutablePointer<Pointee> : Strideable, Hashable {

    public var pointee: Pointee { get nonmutating set }
    public subscript(i: Int) -> Pointee { get nonmutating set }

}

Here, the "nonmutating set" means that setting the property does not mutate the state of the pointer variable itself.

Therefore, a ptr.pointeenew value can be assigned, even if it ptr is a constant, and the same is true for the indexer:

let ptr = UnsafeMutablePointer<Int>.allocate(capacity:1)
ptr.pointee = 1
ptr[0] = 2
0
source

ptr , UnsafeMutablePointer, , , . , - let, .

+2

.

, , .

, pointee ( ), .

+2

, , , . , , , .

+1

Source: https://habr.com/ru/post/1689241/


All Articles