Difference between Structure() and Structure.init()

In several tutorials a struct (or class) is called using the format:

let structure = Structure.init()

Is there any difference between that and just calling

let structure = Structure()

It just seems a long winded way of saying the same thing. Am I missing something?

1 Like

There is no difference. Second option can be convenient when you have some let t: Structure.Type = … and want to call t.init() on the type.

Or you can write let s: Structure = .init(). Especially if you don’t have to write type explicitly, but it is already known:

class C {
    let s: Structure
    init() {
        self.s = .init()
    }
}
4 Likes

Explicitly referencing .init can also be useful in combination with map, for example

let input = """
first
second
third
fourth
"""

let substrLines = input.split(separator: "\n")
// let strLines = substrLines.map(String) <- Compiler error "No exact matches in call to instance method 'map'"
let strLines = substrLines.map(String.init)
// Could also write this as:
let strLines2 = substrLines.map { String($0) }
5 Likes