1

in C# it is possible to do something like:

myClass() { var1 = 5, var2 = 6 }

So I can initialize variables of that class without the need of having a constructor for these variables like:

myClass(var1: 5, var2: 6)

Is it possible to do something similar in Swift?

5
  • 1
    struct will auto generate constructor for you. Commented Jun 27, 2016 at 21:48
  • 1
    If all properties have an initial value (and you don't provide any initializer yourself) then the compiler creates a default parameterless initializer for you, see "Default Initializers" in the Swift book. Commented Jun 27, 2016 at 21:53
  • ... and this applies to both structs and classes. In addition, structs can have a default memberwise initializer. So what exactly are you looking for? Commented Jun 27, 2016 at 22:00
  • @Bryan Chen: Ok, thanks, but I need a class Commented Jun 27, 2016 at 23:49
  • @Martin R: I am sorry but I am not exactly sure what you are referring to. I want to set some (not all and varying) properties of the class to an individual value when I create it. Commented Jun 27, 2016 at 23:49

1 Answer 1

2

Yes, for structs. No for classes.

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

Comments

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.