There is something that I don\'t entirely understand when it comes to mutating value types in Swift.
As the \"The Swift Programming Language\" iBook states:
One more variant
struct MyStruct {
var myVar = "myVar"
let myLet = "myLet"
}
func testMutateString() {
//given
let myStruct = MyStruct()
//Change var
//when
var myStructCopy = myStruct
myStructCopy.myVar = "myVar changed 1"
//then
XCTAssert(myStructCopy.myVar == "myVar changed 1")
//Change let
//when
withUnsafeMutableBytes(of: &myStructCopy) { bytes in
let offset = MemoryLayout.offset(of: \MyStruct.myLet)!
let rawPointerToValue = bytes.baseAddress! + offset
let pointerToValue = rawPointerToValue.assumingMemoryBound(to: String.self)
pointerToValue.pointee = "myLet changed"
}
//then
XCTAssert(myStructCopy.myLet == "myLet changed")
}
[Swift types]