Programming in Swift - Part 51: Part 6: Classes: | Ray Wenderlich

Practice working with classes and understanding when to use them vs. structures, through a hands-on challenge.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/3535-programming-in-swift/lessons/51

The difference between a struct and a class I thought was the use of a init. Well I know that one is based on group and the other is individual but the init needs to be added to a class wile a strut you don’t, or so I thought. Why does this differ from the previous lesson?

Hey @lac_ekim!

Basically, it’s not 100% accurate that init is required for classes, but not for structures.

A better way to think of it is structures automatically create a hidden, default init for you (to make your life easier as a programmer) while classes do not (you always must write it yourself).

So for structures, you can override the default init if you’d like by writing of your own.

Although it’s true that structures have that handy feature of creating a default initializer automatically for you, that’s not the main difference between classes and structures. The main difference is that structures are value types, and classes are reference types.

Value types vs. reference types takes some to get your mind wrapped around. I’d recommend re-watching the previous video (from 1:30 onwards) for more details: https://videos.raywenderlich.com/courses/90-programming-in-swift/lessons/50

I hope that helps and let me know if you have any other questions! :]

Thank you, I am almost there understanding the language. the difference between a init, required init and the other
 is what im tackling. On top of that it the reasoning behind them of why you use one instead of another. I really thank you for everything
you, your team have been a great help, TY.

currently im trying to figure out this
"class Person {
var firstName: String
var lastName: String

init(firstName: String, lastName: String) {
    self.firstName = firstName
    self.lastName = lastName
}

}

class Animal {
var name: String

(why this has a required )init(name: String) {
    self.name = name
}

func speak() {
    
}

}
"

IDK if this will look like a real question to you but I must have a base built to truly get it. I must use you, the forums, the swift handbook.

keep up the GREAT job bud bud.

Hi again! If I understand your question right, it sounds like you’re wondering why the Animal class has a required init method.

This is because the designers of Swift wanted to make sure that when a class is initialized, all of the properties of that class have an initial value.

So they force you to create an initializer that sets all of the properties in the class (like name) to an initial value. That way, after you create an instance of the class, you can be assured that all of the properties on your class have an initial value (rather than a potential undefined state).

I hope that helps!

This topic was automatically closed after 166 days. New replies are no longer allowed.

Please see my code on below:

struct List {
    let name: String
    var movies: [String] = []
    init(name: String) {
        self.name = name
    }

    func print() {
        for movie in movies {
            Swift.print(movie)
        }
        Swift.print("\n")
    }
}

struct User {
    var lists: [String: List] = [:]
    mutating func addList(list: List) {
        lists[list.name] = list
    }
    func list(forName name: String) -> List? {
        return lists[name]
    }
}

var jane = User()
var theMovieList = List(name: "Comedy Movies")
jane.list(forName: "Comedy Movies")?.movies.append("Jumanji")

I got the compile error according to the last line of above code:

error: challenge-classes-vs-structures-starter.playground:64:38: error: cannot use mutating member on immutable value: function call returns immutable value
jane.list(forName: “Comedy Movies”)?.movies.append(“Jumanji”)

It will be fine if I use:

jane.lists["Comedy Movies"]?.movies.append("Jumanji")

Why movies became immutable value when I use
List(forName:) -> List? function?

Arrays in Swift are value types, not reference types; once you pass them into a function, you cannot modify the contents of that original array.

(personal opinion) While I think it’s great that you can mutate structs using the mutating keyword, I tend to use classes a lot more, especially when it comes to this type of ‘nested’ set up.