Created
November 3, 2021 10:20
-
-
Save vitonzhangtt/a89949daa92641b7ea55d4e2cbaca436 to your computer and use it in GitHub Desktop.
The assignment operation of struct variable with inner reference.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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