4

Quote from the office Swift Document

All structure have an automatically-generated memberwise initializer, which you can use to initialize the member properties of new structure instances ...

Question 1: What is so special about the default initializer, why can't it be simply called the default initializer? Why adding the "memberwise"? Is it because it lists out all the member properties defined in the Structure. And you also have to follow the order defined inside the Structure when creating an instance.

Question 2 Is there any other special initializers who have their own special name? If so, what they look like.

[Note Part I:] For further discuss with Mr. Vadian

enter image description here

[Note Part II:]

enter image description here

SLN
  • 4,772
  • 2
  • 38
  • 79

2 Answers2

5

Regarding question 1:

There is an irrevocable law in Swift:

Classes and structures must set all of their stored properties to an appropriate initial value by the time an instance of that class or structure is created. Stored properties cannot be left in an indeterminate state.

We are talking about structs:

When creating a struct you can use the default initializer (the pair of parentheses) if all properties have a default value.

If you just declare the properties without a default value, the compiler creates an implicit memberwise initializer – which you have to use – to make sure to assign a default value to each property in a very convenient way

vadian
  • 274,689
  • 30
  • 353
  • 361
  • Thanks a lot! Vadian. So the compiler only creates an Implicit memberwise initializer to notice you what to create in the proper and correct way. – SLN Jun 23 '16 at 20:40
  • I think I understand a little bit what you said. If I create a Structure without creating any default value for the properties. When I create an instance of that Structure, the compiler will automatically generate a "memberwise initializer" notice for me. (Please see the note part of the question) – SLN Jun 23 '16 at 20:44
  • Yes, exactly, but it's not really a notice, it's just a suggestion how to fulfill the requirements – vadian Jun 23 '16 at 20:48
  • Not notice,,hmmm.. and is is called the **implicit** Memberwise initialzer.. Makes a lot of sense for me now! Thanks so much, Always can learn staff from your guys' help! – SLN Jun 23 '16 at 20:50
  • It's only called **memberwise initializer** – vadian Jun 23 '16 at 20:52
  • I think if I declare the properties with a default value it can also generate the memberwise initializer (please see the note part II) . Hence, it then has a default initializer `test() ` and the memberwise initializer 'test(a: Int, b: Int)' – SLN Jun 23 '16 at 21:02
  • 1
    Yes, the memberwise initializer is created also in this case to be able to assign custom different values easily. – vadian Jun 23 '16 at 21:06
2

This seems primarily opinion based, and not a good fit for SO, but here's my 2 cents:

  1. Default sounds a lot like Designated, which is a completely orthogonal concept.

  2. Not "special", but there are at least 2 different kinds of initializers: designated and convenience.

Alexander
  • 59,041
  • 12
  • 98
  • 151
  • 1
    Thanks. I haven't heard anything bout the designated and convenience initializer. Good to know that they do exist and what they called. More staff to learn now ") – SLN Jun 23 '16 at 20:23
  • If you feel this answer satisfies your question, please accept it :) – Alexander Jun 23 '16 at 20:27
  • Designated and convenience initializers are only for classes. This question was in regards to a struct. – Lee Gillen Jan 18 '19 at 20:52
  • @LeeGillen Even still, it would leading to confusing terminology. – Alexander Jan 18 '19 at 20:54