The Difference in Creating an Object with () and init()?

Initialise!!

Steven Curtis

--

Photo by Tevarak Phanduang on Unsplash

Difficulty: Beginner | Easy | Normal | Challenging
This article has been developed using Xcode 11.5, and Swift 5.2.4

Prerequisites:

The Question

You might have seen the two alternatives that follow for instantiating a class: Person(name: "Dave") and Person.init(name: "Dave") . What is the difference, and which should we use?

This article seeks to give the solution

An Example

Imagine you have a struct. This struct is going to store a person's name.

If you need a reminder about initializers please do take a look at this guide . I'll wait.

Are you back?

Right.

This struct can be created in Swift by using:

now there are two alternative ways to create an instance of this object in Swift:

--

--