Last Updated: February 25, 2016
·
352
· kkoval

Swift Immutable Model

Few rules to make code CLEAN:

1. Declare constant properties var

2. Put only fields deceleration and constructors to model declaration

3. Put model logic to Extensions

Code Example.

class Person {
  let firstname: String
  let lastname: String
  let age: Int

  init(firstname: String, lastname: String, age: Int) {
    self.firstname = firstname
    self.lastname = lastname
    self.age = age
  }
}

Move all login into extensions

extension Person {    
  func age(age: Int) -> Person {
    return Person(firstname: firstname, lastname: lastname, age: age)
  }
}