Object Oriented Programming

Instantiation in Swift

Instantiation is used to create an object instance from a class (also known as initialization).


Syntax
var classObject = className(paramName: paramValue, paramName2: paramValue2)

Notes

External parameter names must be provided to instantiate an object.


Example
//class looks like this
public class Car {
    var make: String
    init(make: String) {
        self.make = make
    }
}

//instantiation, elsewhere in the program
var myCar = Car(make: "Honda") //passing in Honda into the make variable

Add to Slack

< Initializer   |   Type Methods >

© 2019 SyntaxDB. All Rights Reserved.