1

The below code, where one is for Struct, the other is for Class.

I'm wondering why we need init for class and not struct (although we can add one to it)? Is there any technical reason behind that an init is required in class, but more relax in struct?

struct StructTest {
    private let value: Int
}

class ClassTest {
    private let value: Int

    init(value: Int) {
        self.value = value
    } 
}
2
  • 1
    This should answer your question: stackoverflow.com/questions/36036202/… Commented Nov 14, 2020 at 5:37
  • Nice @Cristik. That's nice. Commented Nov 14, 2020 at 5:56

2 Answers 2

7

Structs still have an initializers. The only differences is that in some cases the compiler will synthesize a "default member-wise initalizer" for you.

In this case, it created one with the signititure private init(value: Int) (private because your struct has a private field)

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

5 Comments

Thanks @Alexander. I understand for Struct, a default is given. But why can't they do it for Class as well? Is there a technical reason or philosophical reason that a default memberwise initializer cannot or should not be automatically synthesized for a class?
@Elye I think it's because classes aren't really defined by their variables. They're less "value-like" in that their identity is defined by their memory address and public API, unlike structs which are identified solely by their members. It's not a great justification IMO, because structs can also have private variables
Thanks, @Alexander. Sounds like more of a philosophical reason than a technical one. Thanks for the explanation. I just want to thoroughly understand the rationale behind it.
Yep. Although if you find yourself frequently writing member-wise initializers for classes, perhaps you're not not taking advantage of structs enough
Cool! The rule always goes for struct first.
2

In Swift, both struct and class are used to define custom types, but they have some key differences in behavior. One of these differences is that structs in Swift automatically receive a memberwise initializer by default, whereas classes do not.

The reason behind this design choice is rooted in the fundamental differences between structs and classes. Structs in Swift are value types, which means that when you create an instance of a struct and assign it to a variable or pass it as a function argument, a copy of the entire value is made. On the other hand, classes are reference types, and when you work with them, you are actually dealing with references to the underlying instance.

Given this distinction, the memberwise initializer for structs allows you to easily initialize all the properties of a struct using a single initializer. Since structs are copied when assigned or passed around, having a memberwise initializer simplifies the process of creating new instances.

Classes, being reference types, are often used for more complex scenarios and can have inheritance, deinitializers, and other features that require more explicit initialization logic. Providing a default constructor for classes would require additional considerations and would not be as straightforward as it is for structs.

That said, in Swift, you can still define custom initializers for both structs and classes. If you want a class to have a default constructor, you can define one yourself by providing a parameterless initializer. However, unlike structs, classes do not automatically receive a memberwise initializer.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.