Skip to content

Commit

Permalink
feat(blog): move proof order in reference value
Browse files Browse the repository at this point in the history
  • Loading branch information
theodorusclarence committed May 10, 2022
1 parent 9454631 commit e193fab
Showing 1 changed file with 27 additions and 27 deletions.
54 changes: 27 additions & 27 deletions src/contents/blog/swift-value-reference.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,33 @@ Key Point: **It will share a single copy**
height={659}
/>

### Effect of Sharing A Single Copy

I believe you already guessed correctly how it will behave. If we **mutate** one variable, **both will be affected**.

```swift {8,9,11,12}
class Animal {
var legs = 4
}

var sheep = Animal()
var cow = sheep

// mutating cow's property
cow.legs = 3

print(sheep.legs) // 3
print(cow.legs) // 3
```

<CloudinaryImg
mdx
publicId='theodorusclarence/blogs/swift-value-reference/reference-copy-effect_khsni9'
alt='reference-copy-effect'
width={824}
height={444}
/>

### Proof

To prove that it is sharing a single copy, we can use `===` ([identity equality](https://developer.apple.com/documentation/swift/1538988)). It will return true if two reference point to the same object instance.
Expand Down Expand Up @@ -240,33 +267,6 @@ print(sheep === newSheep) // false

When in doubt, draw the wire analogy to help you. I'm using [excalidraw](https://excalidraw.com/) for the illustration

### Effect of Sharing A Single Copy

I believe you already guessed correctly how it will behave. If we **mutate** one variable, **both will be affected**.

```swift {8,9,11,12}
class Animal {
var legs = 4
}

var sheep = Animal()
var cow = sheep

// mutating cow's property
cow.legs = 3

print(sheep.legs) // 3
print(cow.legs) // 3
```

<CloudinaryImg
mdx
publicId='theodorusclarence/blogs/swift-value-reference/reference-copy-effect_khsni9'
alt='reference-copy-effect'
width={824}
height={444}
/>

## Additional Emphasize

I need to emphasize this in case you're coming from **a JavaScript** background.
Expand Down

1 comment on commit e193fab

@vercel
Copy link

@vercel vercel bot commented on e193fab May 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.