Why are the results of the two prints different?
Swift , , a.key
getter. . , , , .
, , , A
key
, ( ).
key
, ( ).
, key
final
, :
class A {
final var key = "aaa"
}
var a = A()
aaa(&a.key)
bbb(&a.key)
key
, getter.
, . , , . , , , , , , (, , , ).
. Swift , , , . Swift C- ( ):
C- Swift, . :
- , . , . , string , . . , : KVO.
, key
A
, .
bbb
, aaa
,
, , , -O- . .
( , Swift , Swift, , swiftc
)
- , ( ). aaa
, " ", , .
- A
, getter a.key
. a.key
-, , .
, a.key
, var key = arc4random()
, , a.key
.
, , ( ) - , , .
inout
UnsafeMutable(Raw)Pointer
:
withUnsafePointer(to:_:)
, ( , ), inout
. , inout
.
inout
, UnsafeRawPointer
. , inout
, pointee
UnsafeRawPointer
.
- inout
, :
, , getter. , , (, ) .
, .
, , , , final
( ). , . copy-on-write, , - .
, Swift - materializeForSet
. , , , , .
- , inout
- a.key
materializeForSet
, , , .
materializeForSet
, , , UnsafeRawPointer
. aaa
bbb
, UnsafeMutable(Raw)Pointer
( ), .
func aaa(_ key: UnsafeMutableRawPointer) {
print(key)
}
func bbb(_ key: UnsafeMutableRawPointer) {
print(key)
}
class A {
var key = "aaa"
}
var a = A()
aaa(&a.key)
bbb(&a.key)
, , , , .