# Structures and Classes : Quick Reference Guide

**URL:** <https://forums.swift.org/t/structures-and-classes-quick-reference-guide/62889>\
**Category:** Using Swift\
**Tags:** stuctures, classes\
**Created:** [February 1, 2023, 7:45pm UTC](https://forums.swift.org/t/structures-and-classes-quick-reference-guide/62889 "2023-02-01T19:45:12Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marie.B.B](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/marie.b.b/32/28326_2.png) [@Marie.B.B](https://forums.swift.org/u/Marie.B.B)\
**Post date:** [February 1, 2023, 7:45pm UTC](https://forums.swift.org/t/structures-and-classes-quick-reference-guide/62889/1 "2023-02-01T19:45:12Z")

</div>

I made a Quick Reference Guide for Structures and Classes.  
(One Pager.)  
What thinks?  
🌈🙏🌈

 ![](https://global.discourse-cdn.com/swift/original/3X/b/c/bcea6b73967edf401b73c2e4d8c920a231eebe7d.png)

---

<div class="post-metadata">

**Author:** ![taylorswift](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/taylorswift/32/43413_2.png) [@taylorswift](https://forums.swift.org/u/taylorswift)\
**Post date:** [February 1, 2023, 8:18pm UTC](https://forums.swift.org/t/structures-and-classes-quick-reference-guide/62889/2 "2023-02-01T20:18:03Z")

</div>

great work! a few notes:

- all swift types, including `struct`s and `enum`s support typecasting, because all (copyable) types can conform to protocols.

```swift
enum E:Error
{
    case e
}

let erased:any Error = E.e

if let e:E = erased as? E
{
    ...
}

```

- `@_moveOnly` `struct`s do support deinitializers, at least according to the rumors.

- letting an optional instance property use implicit initialization is a bad idea for performance reasons. write out `var name:String? = nil` explicitly, or better, assign it from an `init`. see this thread for an explanation why: [What is a 'variable initialization expression'?](https://forums.swift.org/t/what-is-a-variable-initialization-expression/52132)

- memberwise `struct` initializers will never be more than `internal`, to expose a `public` initializer, you have to write it out explicitly.

- “a value type sends a copy whenever it is passed” is largely meaningless in practical swift development, both because of the prevalence of inlining, and because many structs are just values of references, which could possibly be copy-on-write references to make them behave as if they were values from a mutation perspective (e.g., `[Int]`).

- a `class` value _is_ literally a pointer. this is actually quite important when writing generic data structures, because if you can constrain the element type parameter to `AnyObject` the compiler is able to optimize layout more aggressively.

---

<div class="post-metadata">

**Author:** ![jeremyp](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jeremyp/32/3552_2.png) [@jeremyp](https://forums.swift.org/u/jeremyp)\
**Post date:** [February 2, 2023, 4:35pm UTC](https://forums.swift.org/t/structures-and-classes-quick-reference-guide/62889/3 "2023-02-02T16:35:33Z")

</div>

> Structs and Classes are types. Types start with a capital letter. Properties and methods start with a lower case letter.

I'd make it clear that that is just a style convention in Swift. It's not strictly part of the language, unlike, say, Haskell.

---

<div class="post-metadata">

**Author:** ![AlexanderM](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alexanderm/32/9781_2.png) [@AlexanderM](https://forums.swift.org/u/AlexanderM)\
**Post date:** [February 2, 2023, 5:38pm UTC](https://forums.swift.org/t/structures-and-classes-quick-reference-guide/62889/4 "2023-02-02T17:38:09Z")

</div>

> [@jeremyp](#):
>
> style convention in Swift. It's not strictly part of the language, unlike, say, Haskell.

I wish it was. The consistency PEP 8 in Python and `gofmt` in Go is so nice. 😃

---

<div class="post-metadata">

**Author:** ![AlexanderM](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alexanderm/32/9781_2.png) [@AlexanderM](https://forums.swift.org/u/AlexanderM)\
**Post date:** [February 2, 2023, 5:39pm UTC](https://forums.swift.org/t/structures-and-classes-quick-reference-guide/62889/5 "2023-02-02T17:39:46Z")

</div>

Boy, technical writing is hard, but I really wish we had a good resource to explain reference vs value semantics. Most mentions of "structs are value types and classes are reference types" are woefully simplistic and just cause further confusion down the road :/

See also: [Question about CoW on value type - #10 by AlexanderM](https://forums.swift.org/t/question-about-cow-on-value-type/62640/10)

---

<div class="post-metadata">

**Author:** ![JohnBlackburne](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/johnblackburne/32/8611_2.png) [@JohnBlackburne](https://forums.swift.org/u/JohnBlackburne)\
**Post date:** [February 2, 2023, 11:36pm UTC](https://forums.swift.org/t/structures-and-classes-quick-reference-guide/62889/6 "2023-02-02T23:36:19Z")

</div>

I was trying to 'explain' reference vs. value types the other day. I wrote a paragraph, linked to a page on Wikipedia, then waded into actual code. It's hard to explain as everyone with even basic OOP programming knowledge knows the difference already. They just probably aren't used to thinking about it.

For the quick reference guide; classes being reference types and structs being value types is what makes them different. Everything else, such as classes having inheritance, reference counting, casting between classes, follows from it. I would mention reference v. value at the start.

---

<div class="post-metadata">

**Author:** ![Marie.B.B](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/marie.b.b/32/28326_2.png) [@Marie.B.B](https://forums.swift.org/u/Marie.B.B)\
**Post date:** [February 3, 2023, 7:52am UTC](https://forums.swift.org/t/structures-and-classes-quick-reference-guide/62889/7 "2023-02-03T07:52:39Z")

</div>

Firstly, thank you, @taylorswift, @AlexanderM, @JohnBlackburne, and @jeremyp for your comments.

Secondly a bit of context... I am a beginner at Swift although I have coded in several other languages and I'm excited to get up to speed quick. This exercise is to summarize and make Quick Reference Guides (QRG) from the pages found at `https://docs.swift.org/` which is published by `Apple Inc.` so, I guess those pages are coming "straight from the horse's mouth". Anything in the QRG is said by Apple.

The goal of the QRG is to be a "Cheat Sheet" of sorts. Something quickly and easily referenced to check knowledge of a programming fact and if deeper explanations are needed, by all means see the source doc noted at the bottom of the QRG. I'm aiming for a "one pager" to summarize the contents of that long page in the source. So they might be "light on verbose explanation" but heavy on "this is how it works quickly and tersely as a reminder or review". I'm learning a lot in the process of making these and I plan on using them myself and I am happy to share them with the community because they don't seem to exist anywhere. (If you google "Swift Cheat Sheet" you will find one good four page PDF and one less-than-good document that is one page long, and that's about it.)

I'm trying not to do too much editing apart from condensing the Apple Documentation, but I like your suggestion, @JohnBlackburne to put the Value Type vs. Reference Type distinction at the beginning. There is a great Value Type vs. Reference Type explanation in the source page noted in the footer:

[https://docs.swift.org/swift-book/LanguageGuide/ClassesAndStructures.html](https://docs.swift.org/swift-book/LanguageGuide/ClassesAndStructures.html)

...

@taylorswift, I would be happy to incorporate your edits, but I would like to understand them completely first before doing so.

Regarding deinit, I found this page:

> <https://stackoverflow.com/questions/50399028/why-dont-structs-have-deinitializers-in-swift-like-classes>

Regarding @inline, I found this page:

> **[The Forbidden @inline Attribute in Swift](https://swiftrocks.com/the-forbidden-inline-attribute-in-swift)**
>
> The @inline attribute is one of those obscure things in Swift - it's nowhere to be found in Apple's docs, doesn't help you write cleaner code and has no purpose but to help the compiler make optimization decisions, but it's related to a pretty...

Regarding value semantics I found this page:

[Value SEMANTICS (not value types!)](https://academy.realm.io/posts/swift-gallagher-value-semantics/#:~:text=Value%20semantics%20is%20about%20interface,reference%20type%20is%20about%20implementation).

And regarding pointers, I was just quoting "The Apple Textbook" via the source page in the footer.

@taylorswift I feel like you know what you are talking about and are a Swift God Tier Level. 👏

...

@jeremyp Edit Accepted. Thank you. I added a bit about naming capitalization being a common human convention and not hard-coded into Swift.

...

Ok, so here is version 2.0, with color coding similar to the source document, and Value/Reference discussion toward the front :

 ![](https://global.discourse-cdn.com/swift/original/3X/3/6/36b7eedca393db5295c80a71798b6d5df86b6a5b.png)  
Also, here is an updated, colorized release (Thank you Ted Turner.) of the first QRG on Basic Operators :

 ![](https://global.discourse-cdn.com/swift/original/3X/f/7/f717e99bcb29f3b518d99974851f8b3535eb3365.png)  
What thinks?

🌈🙏🌈

---

<div class="post-metadata">

**Author:** ![jeremyp](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jeremyp/32/3552_2.png) [@jeremyp](https://forums.swift.org/u/jeremyp)\
**Post date:** [February 3, 2023, 8:38am UTC](https://forums.swift.org/t/structures-and-classes-quick-reference-guide/62889/8 "2023-02-03T08:38:07Z")

</div>

> [@AlexanderM](#):
>
> I wish it was. The consistency PEP 8 in Python and `gofmt` in Go is so nice.

I'm glad it's not. Whilst I adhere to Swift style identifiers it in all languages where possible, there are other aspects of standard Swift style that I absolutely loathe e.g. K&R braces. Style should not be dictated by the language designers IMO.

Anyway, this is off topic, so I'll say no more on the subject.

---

<div class="post-metadata">

**Author:** ![jeremyp](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jeremyp/32/3552_2.png) [@jeremyp](https://forums.swift.org/u/jeremyp)\
**Post date:** [February 3, 2023, 8:50am UTC](https://forums.swift.org/t/structures-and-classes-quick-reference-guide/62889/9 "2023-02-03T08:50:31Z")

</div>

> [@Marie.B.B](#):
>
> What thinks?

On a pedantic note, Swift's remainder operator is technically not modulo. It gives a different result to what I would regard as the One True Modulo for negative dividends. You could probably just add that the result has the same sign as the dividend e.g. `-7 % 4 == -3`.

---

<div class="post-metadata">

**Author:** ![Marie.B.B](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/marie.b.b/32/28326_2.png) [@Marie.B.B](https://forums.swift.org/u/Marie.B.B)\
**Post date:** [February 3, 2023, 6:02pm UTC](https://forums.swift.org/t/structures-and-classes-quick-reference-guide/62889/10 "2023-02-03T18:02:46Z")

</div>

@jeremyp Thank you. I changed "(Also known as : Modulo.)" to "(Similar to : Modulo)" because I have heard people call "%" Modulo commonly all the time.

And I added:

```swift
// The result has the same sign as the dividend.

```

Again, thank you, and thank everyone for a rigorous look at this fun and educational and hopefully useful-to-someone-besides-me project.

🌈🙏🌈
