Playing around with Swift, coming from a Java background, why would you want to choose a Struct instead of a Class? Seems like they are the same thing, with a Struct offering less functionality. Why choose it then?
|
|
According to the very popular WWDC 2015 talk Protocol Oriented Programming in Swift (video, transcript), Swift provides a number of features that make structs better than classes in many circumstances. Structs are preferable if they are relatively small and copiable because copying is way safer than having multiple reference to the same instance as happens with classes. This is especially important when passing around a variable to many classes and/or in a multithreaded environment. If you can always send a copy of your variable to other places, you never have to worry about that other place changing the value of your variable underneath you. With Structs there is no need to worry about memory leaks or multiple threads racing to access/modify a single instance of a variable. Classes can also become bloated because a class can only inherit from a single superclass. That encourages us to created huge superclasses that encompass many different abilities that are only loosely related. Using protocols, especially with protocol extensions where you can provide implementations to protocols, allows you to eliminate the need for classes to achieve this sort of behavior. The talk lays out these scenarios where classes are preferred:
It implies that structs should be the default and classes should be a fallback. On the other hand, The Swift Programming Language documentation is somewhat contradictory:
Here it is claiming that we should default to using classes and use structures only in specific circumstances. Ultimately, you need to understand the real world implication of value types v.s. reference types and then you can make an informed decision about when to use structs or classes. Also keep in mind that these concepts are always evolving and The Swift Programming Language documentation was written before the Protocol Oriented Programming talk was given. My personal advice, is to always default to using a struct because they greatly reduce complexity and fallback to classes if the Struct becomes very large or requires some feature that structs and protocols cannot provide, most notably the ability to have multiple variables reference the same data. |
|||||||||||||||||||||
|
|
One big advantage is performance. Since struct instances are allocated on stack, and class instances are allocated on heap, structs can be drastically faster. Consider the following example, which demonstrates 2 strategies of wrapping
and measure the performance using
UPDATE (1 June 2014): As of Swift 1.2, XCode 6.3.2, running Release build on iPhone 5S, iOS 8.3, averaged over 5 runs
That's 900 times faster. OLD RESULTS In release build (on my Macbook), the first test takes 1.10082 sec, while the second one takes 0.02324 sec. That's 50 times faster! (results are roughly the same in debug build). |
|||||||||||||||||||||
|
Similarities between structs and classes.I created gist for this with simple examples. https://github.com/objc-swift/swift-classes-vs-structures And differences1. Inheritance.structures can't inherit in swift. If you want
Go for an class. 2. Pass BySwift structures pass by value and class instances pass by reference. Contextual DifferencesStruct constant and variables Example (Used at WWDC 2014)
Defines a struct called Point.
Now if I try to change the x. Its a valid expression.
But if I defined a point as constant.
In this case entire point is immutable constant. If I used a class Point instead this is a valid expression. Because in a class immutable constant is the reference to the class itself not its instance variables (Unless those variables defined as constants) |
|||||||||
|
|
Here are some other reasons to consider:
To get this in a class, you would have to add the initializer, and maintain the intializer...
|
|||||||||||||||||||||
|
|
Some advantages:
|
|||||||||||||||||
|
|
With classes you get inheritance and are passed by reference, structs do not have inheritance and are passed by value. There are great WWDC sessions on Swift, this specific question is answered in close detail in one of them. Make sure you watch those, as it will get you up to speed much more quickly then the Language guide or the iBook. |
|||||||||||||
|