Home Ask Login Register

Developers Planet

Your answer is one click away!

user1107173 February 2016

Swift: Reducing the length of Init methods

I would like to reduce the length of init method.

struct Person {

    var id: Int
    var firstName: String
    var lastName: String
    var vehicle: String
    var location: String
    var timeZone: String

    init (id: Int, firstName: String, lastName: String, vehicle: String, location: String, timeZone: String ) {
        self.firstName = firstName
        self.lastName = lastName
        self.vehicle = vehicle
        self.location = location
        self.timeZone = timeZone

Below is an instance of Person I am creating. I have to pass in the value of every single variable inline.

let person = Person(id: 22, firstName: "John", lastName: "Doe", vehicle: "Chevy", location: "Dallas", timeZone: "CST")

Question: How can I shrink the length of init? In Obj-C I used to create a data model class. Populate it's variables and then pass the entire class, reducing the length of the init method.


Person *person = [Person new];
person.id = 22;
person.firstName = "John";
person.lastName = "Doe";
person.vehicle = "Chevy";
person.location = "Dallas";
person.timeZone = "CST"

Person *person = [Person initWithPerson:person];

What's an equivalent way in Swift to reduce the length of init without having to initialize every single variable inline? I know tuples is one way, is there any other best practice?


EmilioPelaez February 2016

Like it was mentioned in the comments, an initializer will be created for you, and it'll look like this:

Person(id: Int?, firstName: String?, lastName: String?, vehicle: String?, location: String?, timeZone: String?)

However, you can also do this:

var person = Person()
person.id = 100
person.firstName = "Name"

Like you used to do in ObjC. Note that person was declared as var, because if it was declared as let, you wouldn't be able to mutate it.

vadian February 2016

Using a struct you actually don't need an initializer

struct Person {
  var id : Int?
  var firstName: String?
  var lastName: String?
  var vehicle: String?
  var location: String?
  var timeZone: String?

var person = Person()
person.id = 22
person.firstName = "John"
person.lastName = "Doe"
person.vehicle = "Chevy"
person.location = "Dallas"
person.timeZone = "CST"

You can do the same with non-optionals

struct Person {
  var id = 0
  var firstName = ""
  var lastName = ""
  var vehicle = ""
  var location = ""
  var timeZone = ""

Consider also the benefit of an initializer to declare (read-only) constants

struct Person {
  let id : Int
  let firstName : String
  let lastName : String
  let vehicle : String
  let location : String
  let timeZone : String

In this case you have to use the implicit memberwise initializer.

let person = Person(id: 22, firstName: "John", lastName: "Doe", vehicle: "Chevy", location: "Dallas", timeZone: "CST")

appzYourLife February 2016

Just remove the initializer!

struct Person {
    let id: Int
    let firstName: String
    let lastName: String
    let vehicle: String
    let location: String
    let timeZone: String

Now you can use the memberwise initializer

    id: 87112,
    firstName: "Walter",
    lastName: "White",
    vehicle: "2004 Pontiac Aztek",
    location: "Albuquerque",
    timeZone: "UTC-07:00"

Structure types automatically receive a memberwise initializer if they do not define any of their own custom initialisers.

The Swift Programming Language

DO NOT use var

As you can see I replaced var with let. Unless you need to change some properties of a Person after the value has been created, I suggest you to use let. Otherwise you are free to use var. This way the compiler will prevent unwanted changes.

DO NOT use Optionals

I don't know the business logic of your app, however if a Person must have all that 6 properties always populated, don't make them optionals. Otherwise every time you need to use a Person value the compiler will force you to check if that optional has a value.

DO NOT use Implicitly Unwrapped Optionals

Seriously. There are a few cases where they are useful and a model value is not one of them

Post Status

Asked in February 2016
Viewed 2,785 times
Voted 9
Answered 3 times


Leave an answer

Quote of the day: live life