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.pointee
new 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
source
share