Skip to content

Commit

Permalink
feat(blog): update swift-value-reference
Browse files Browse the repository at this point in the history
fix typos, add reference types inside of value types section
  • Loading branch information
theodorusclarence committed May 10, 2022
1 parent e193fab commit 3f87ffb
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions src/contents/blog/swift-value-reference.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -262,12 +262,46 @@ Then we can compare them using identity equality

```swift
print(sheep === cow) // true
print(sheep === newSheep) // false
print(sheep === pig) // false
```

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

## Additional Emphasize
## Reference Types Inside of Value Types

Important thing to note is: If you are referencing a class inside of a struct, then **that variable will still behave like the reference type**

```swift
class Leg {
var count = 4
}

struct Animal {
var name: String
var legs = Leg()
}

var sheep = Animal(name: "Sheep")
var cow = sheep

sheep.legs.count = 3

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

// referencing the same class
print("\(sheep.legs === sheep.legs)") // true
```

<CloudinaryImg
mdx
publicId='theodorusclarence/blogs/swift-value-reference/reference-inside-value_fg6n91'
alt='reference-inside-value'
width={766}
height={662}
/>

## Additional Emphasis

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

Expand Down

1 comment on commit 3f87ffb

@vercel
Copy link

@vercel vercel bot commented on 3f87ffb 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.