Structs vs Classes?

Other than pass by value vs pass by reference, what are the differences between structs and classes? Also, are there any cases where using one would be more advantageous than the other, and if so, when?

Accepted Reply

Other than pass by value vs pass by reference, what are the differences between structs and classes?

I wouldn’t consider the way that parameters are passed to be a key difference between structs are classes. Swift will happily pass a struct by reference if it can prove that doing so doesn’t break the struct’s semantics. Critically, structs (along with enums) are expected to exhibit value semantics, while clases exhibit reference semantics, per KMT’s explanation.

This distinction is trickier than you might think. For example:

  • You can create a struct that uses a class to hold its core data, using copy on write to preserve value semantics. Many of Swift’s standard types do this,

    String
    ,
    Array
    ,
    Data
    , and so on.
  • If you don’t implement copy on write properly, you can end up with struct that has reference semantics! Don’t do that. Anyone reading your code, including Future You™, will be very confused.

  • If you create an immutable class it effectively has value semantics (because all references are equivalent). The Foundation framework has taken advantage of this for years (for example,

    NSUUID
    ).

IMO the key difference between a struct and a class relates to identity. If you use a class, you can maintain a persistent identity for your data over time. This is essentially for modelling various real world constructs.

For example, consider a TCP connection. This exists in the real world, outside of your app. If you use a struct, what does the struct’s value semantics mean? If you make a copy, does it ‘clone’ the TCP connection. Such a concept is nonsense, and thus it only makes sense to use a class.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Replies

Google (and/or swift.org) is really your friend for details, but the short story is In Swift, structs are value types...classes are reference types.


When you copy a struct, you end up with two unique copies of the data.


When you copy a class, you end up with two references to one instance of the data.


Not what I'd exactlyconsider an 'advantage' either way, perhaps more about the data, that difference directly affects your choice between classes or structs depending on your plans for the related data and if you intend to work with (session based?) copies, or (session based?) references.


Is this a homework question....?

A minor difference I find is it is a little more flexible to create a class instance (no need to set all parameters explicitely) ; but there are workaround to achieve the same with strcuts.

My rule of thumb is to use class for complex structures and struct in other cases.

Other than pass by value vs pass by reference, what are the differences between structs and classes?

I wouldn’t consider the way that parameters are passed to be a key difference between structs are classes. Swift will happily pass a struct by reference if it can prove that doing so doesn’t break the struct’s semantics. Critically, structs (along with enums) are expected to exhibit value semantics, while clases exhibit reference semantics, per KMT’s explanation.

This distinction is trickier than you might think. For example:

  • You can create a struct that uses a class to hold its core data, using copy on write to preserve value semantics. Many of Swift’s standard types do this,

    String
    ,
    Array
    ,
    Data
    , and so on.
  • If you don’t implement copy on write properly, you can end up with struct that has reference semantics! Don’t do that. Anyone reading your code, including Future You™, will be very confused.

  • If you create an immutable class it effectively has value semantics (because all references are equivalent). The Foundation framework has taken advantage of this for years (for example,

    NSUUID
    ).

IMO the key difference between a struct and a class relates to identity. If you use a class, you can maintain a persistent identity for your data over time. This is essentially for modelling various real world constructs.

For example, consider a TCP connection. This exists in the real world, outside of your app. If you use a struct, what does the struct’s value semantics mean? If you make a copy, does it ‘clone’ the TCP connection. Such a concept is nonsense, and thus it only makes sense to use a class.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

If you haven't already, check out these most excellent videos which go over in great detail the difference.


https://developer.apple.com/videos/play/wwdc2015/408/

https://developer.apple.com/videos/play/wwdc2015/414/


When Swift first came out, I redid my Objective-C apps as mostly using Swift classes. But value semantics solved numerous issues and greatly reduced complexity. So refactors were done over the years to use more structs and enums where it made sense.

  • Looks like those have been taken down.

Add a Comment