Skip to main content
  1. Posts/

Create a custom struct initializer

·93 words·1 min·
Struct Swift Basics Tips
NSStudent
Author
NSStudent
iOS developer and amateur chess player

Structs create a default initializer when you implement one.

Sometimes you want to create another initializer to facilitate the code.

The problems appear when you try to introduce our initializer. the default constructor disappears. To avoid this problem, the best way is to include our initializer inside an extension.

This way, we’ll keep the two builders

struct Person {
    let name: String
    let birthday: Date
}

extension Person {
    init(name: String) {
        self.name = name
        self.birthday = Date()
    }
}

let person = Person(name: "NSStudent", birthday: Date(timeIntervalSince1970: 426348250))
let baby = Person(name: "john")