Skip to content

Instantly share code, notes, and snippets.

@vitonzhangtt
Created November 3, 2021 10:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vitonzhangtt/a89949daa92641b7ea55d4e2cbaca436 to your computer and use it in GitHub Desktop.
Save vitonzhangtt/a89949daa92641b7ea55d4e2cbaca436 to your computer and use it in GitHub Desktop.
The assignment operation of struct variable with inner reference.
// Inner Reference Type
final internal class CoWInnerClass {
var identifier: Int
init(_ id: Int) {
identifier = id
}
}
// The struct with inner reference type.
struct CoWValueWithRef: CustomStringConvertible {
let ref1 = CoWInnerClass(1)
let ref2 = CoWInnerClass(2)
var description: String {
return String("ref1: \(ObjectIdentifier(ref1)) ref2: \(ObjectIdentifier(ref2))")
}
}
// The test case function
private func testValueWithRef() {
let valueWithRef1 = CoWValueWithRef()
// The assignment operation
let valueWithRef2 = valueWithRef1
// Another struct variable
let valueWithRef3 = CoWValueWithRef()
print("valueWithRef1: \(valueWithRef1)")
print("valueWithRef2: \(valueWithRef2)")
print("valueWithRef3: \(valueWithRef3)")
}
/*
------------------------
Output from console is as the following:
valueWithRef1: ref1: ObjectIdentifier(0x00006000039e9780) ref2: ObjectIdentifier(0x00006000039e9740)
valueWithRef2: ref1: ObjectIdentifier(0x00006000039e9780) ref2: ObjectIdentifier(0x00006000039e9740)
valueWithRef3: ref1: ObjectIdentifier(0x00006000039e9860) ref2: ObjectIdentifier(0x00006000039e9880)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment