struct Test {
var a: String
var b: String
var c: String
}
func copyTest(to: Test) -> Test {
var copy = to
withUnsafePointer(to: ©) { print("\($0)") }
//copy.b = "new world"
withUnsafePointer(to: ©.a) { print("string member a \($0)") }
withUnsafePointer(to: ©.b) { print("string member b \($0)") }
withUnsafePointer(to: ©.c) { print("string member c \($0)") }
return copy
}
var test = Test(a: "hello", b: "world", c: "foo")
withUnsafePointer(to: &test) { print("\($0)") }
withUnsafePointer(to: &test.a) { print("string member a \($0)") }
withUnsafePointer(to: &test.b) { print("string member b \($0)") }
withUnsafePointer(to: &test.c) { print("string member c \($0)") }
test = copyTest(to: test)
withUnsafePointer(to: &test) { print("\($0)") }
withUnsafePointer(to: &test.a) { print("string member a \($0)") }
withUnsafePointer(to: &test.b) { print("string member b \($0)") }
withUnsafePointer(to: &test.c) { print("string member c \($0)") }
print(test.b)
var a = "hello"
var b = a
print(a)
withUnsafePointer(to: &a) { print("\($0)") }
print(b)
withUnsafePointer(to: &b) { print("\($0)") }
func mutate(value: inout String) {
value = "mutated"
}
mutate(value: &a)
print(a)
withUnsafePointer(to: &a) { print("\($0)") }
print(b)
withUnsafePointer(to: &b) { print("\($0)") }
var c = "hello"
withUnsafePointer(to: &c) { print("c initial \($0)") }
func mutation(value: String) -> String {
var value = value
withUnsafePointer(to: &value) { print("c copy \($0)") }
value = "mutated"
return value
}
c = mutation(value: c)
print(c)
withUnsafePointer(to: &c) { print("c mutated \($0)") }
var a = 10
let ptr: UnsafeMutablePointer<Int> = &a