Open Source projects from That Thing in Swift

PermissionScope

PermissionScope

Intelligent iOS permissions UI and unified API — 2,241⭐️ — GithubDocumentation

PermissionScope

Pantry

The missing light persistence layer for Swift — 433⭐️ — GithubDocumentation

Value and Reference Types

Know your type differences and use the right one

Download the demo playground

Download this playground file to play along:
Switch the definition of an object between struct and class to see how the data changes

Since we took a rather long hiatus before iOS 8 rolled out, I figure we would start again with a simple introduction to value and reference types in Swift as well as a test of our new demo playgrounds.

A couple weeks ago, Apple posted a short article about the difference between value and reference types in Swift. The short and long of it is that struct, enum and tuple objects are all value types and objects defined as class are reference types. Value type data is copied when you assign it around whereas reference type data is passed by reference and points to the same underlying data.

We’re used to dealing with reference types in Objective-C. For those of you coming from an Objective-C background, this example should not strike you as surprising:

DemoObject *obj1 = [[DemoObject alloc] init];
obj1.name = @"hello";

DemoObject *obj2 = obj1;
obj2.name = @"what";

// prints “what what”
NSLog(@"%@ %@",obj1.name,obj2.name);

Though it’s possible to pass around values in Objective-C, you’re probably used to this kind of thing because you deal with mostly NSObject subclasses, not a lot of base C types.

The difference in Swift is expressed succinctly in the Value and Reference Types post but I’ll reproduce the above example in Swift to demonstrate.

As a struct (i.e. a value type):

struct DemoObject {
    var name = "hello"
}

var obj1 = DemoObject()
var obj2 = obj1

obj2.name = "what"

// prints “hello what”
print("\(obj1.name) \(obj2.name)")

And as a class (i.e. a reference type):

class DemoObject {
    var name = "hello"
}

var obj1 = DemoObject()
var obj2 = obj1

obj2.name = "what"

// prints “what what”
print("\(obj1.name) \(obj2.name)")

Literally, the only thing different between the two examples is struct and class in the object definition. This effect is best seen for yourself. Download the example playground at the top of this post and try it out yourself.


Questions or comments? Find us on twitter or submit an issue on github

You'll get 1-2 emails per month