23

I am starting to learn the Swift language and am having trouble wrapping my head around Protocols, Structs and Classes.

I come from the Android side of programming, so I believe that Swift Protocols are basically Java Interfaces?

What would be proper use cases for each of these?

0

1 Answer 1

51

These analogies are not "exactly" correct, but this is the gist of it as I understand it

  1. Yes, Protocols are effectively like interfaces

  2. Classes are classes, like in Java/Android, and pretty much any other language

  3. Structs are like classes, but they are passed by-value (copied) when passing them from one variable/function to another. If you're familiar with C# at all, it's implementation of structs is very similar.

Eg:

class Foo {
   ...
}

let x = Foo()
let z = x 

At this point x and z both refer to the same Object in memory, there is only one Foo object

struct Bar {
   ...
}

let a = Bar()
let b = a 

When assigning b, a is copied (think of basically copying the memory block). At this point, there are two independent Bar objects in memory and modifying one doesn't affect the other.

Why is this useful? Sometimes you don't want a shared reference, but mostly for performance reasons. Because structs don't have to all refer to the same object, they don't have to be allocated on the heap. They can instead often be allocated on the stack, which is much faster. Also arrays of structs can be implemented as one big contiguous block of memory which means it's much friendlier on the CPU cache if you want to iterate through it all.

Swift isn't garbage collected, but for garbage collected languages like C# this can mean the garbage collector doesn't have to deal with a lot of objects that it might otherwise have to. Even in swift the struct copying means it can avoid doing the Retain/Release behind the scenes neccessary for ARC, which can help a lot.

The primary use case for structs is when you have lots of immutable "simple data" like a Vector (set of 3 floating point values)

Sign up to request clarification or add additional context in comments.

7 Comments

Very well explained. That answers my question, thanks!
Note that Array, String, and Dictionary are all structs in Swift. The standard library in general leans heavily towards structs and protocols rather than classes.
"let a = Bar() let b = a. When assigning b, a is copied (think of basically copying the memory block). At this point, there are two independent Bar objects in memory and modifying one doesn't affect the other." Not true in implementation. But in use effectively true. When assignment happens for a struct a reference is made to the original. You only get a copy when you actually change b. This is known as copy on write. It saves memory until it's actually needed during run time.
The only reason I bring it up is new programmers to swift might think structs are very memory intensive when being passed around a lot. However because of the copy on write a spect they are not.
@Honey - Scroll down to the "Commentary" section on this answer. It might help explain a few things if you can get past the C# and C++ code in it: stackoverflow.com/questions/22926046/…
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.