Creating a class

To create a new class use the class keyword followed by the class name and a pair of braces. Inside the braces we can declare the class properties.

class Point { var x = 0.0 // sets the default value of x to 0 var y = 0.0 // sets the default value of x to 0 } // this creates a new Point instance using the default initializer var point = Point () point . x = 100 // sets the x property to 100 point . y = 200 // sets the y propery to 200

Initializers

From Apple’s Documentation:

Initialization is the process of preparing an instance of a class, structure, or enumeration for use. This process involves setting an initial value for each stored property on that instance and performing any other setup or initialization that is required before the new instance is ready to for use. You implement this initialization process by defining initializers, which are like special methods that can be called to create a new instance of a particular type. Unlike Objective-C initializers, Swift initializers do not return a value. Their primary role is to ensure that new instances of a type are correctly initialized before they are used for the first time.

Notice that if you do not set the default values of x and y in the above example the compiler will get 3 errors:

class Point { // Class 'Point' has no initializers var x // Type annotation missing in pattern var y // Type annotation missing in pattern }

To fix this we need to set a Type for each property:

class Point { // Class 'Point' has no initializers var x : Double var y : Double }

To create an initializer use the init keyword followed by 0 or more parameters needed for initialization.

class Point { var x : Float var y : Float init ( x : Float , y : Float ) { self . x = x self . y = y } } // Example usage var point = Point ( x : 100 , y : 200 )

Another thing we can do is to set optional types for properties. To set an optional type just add ? after the desired type. If a property is declared with an optional type it will get a default value of nil .

class Point { var x : Float ? var y : Float ? } var point = Point () // both the x and y properties are now set to nil point . x = 100.0 point . y = 200.0

Creating custom initializers

Let’s asume we are using a User class in our project. A user has firstName , lastName and bio properties.

class User { var firstName : String ? var lastName : String ? var bio : String = "I ♡ Swift!" } var user = User () // user = { firstName: nil, lastName: nil, bio: "I ♡ Swift!"}

When a User creates an account the first and last name fields are required and the bio field is optional. In that case we have two case of creating a user. Let’s create an initializer for each case:

class User { var firstName : String var lastName : String var bio : String = "I ♡ Swift!" // no bio provided init ( firstName : String , lastName : String ) { self . firstName = firstName self . lastName = lastName } // bio provided init ( firstName : String , lastName : String , bio : String ) { self . firstName = firstName self . lastName = lastName self . bio = bio } } var me = User ( firstName : "Andrei" , lastName : "Puni" ) // me = { firstName: "Andrei", lastName: "Puni", bio: "I ♡ Swift!"} var silviu = User ( firstName : "Silviu" , lastName : "Pop" , bio : "I f**ing ♡ Swift!!!" ) // silviu = { firstName: "Silviu", lastName: "Pop", bio: "I f**ing ♡ Swift!!!"}

Or another thing we can do is give an implicit value to the bio parameter of the initializer

class User { var firstName : String var lastName : String var bio : String init ( firstName : String , lastName : String , bio : String = "I ♡ Swift!" ) { self . firstName = firstName self . lastName = lastName self . bio = bio } } var me = User ( firstName : "Andrei" , lastName : "Puni" ) // me = { firstName: "Andrei", lastName: "Puni", bio: "I ♡ Swift!"} var silviu = User ( firstName : "Silviu" , lastName : "Pop" , bio : "I f**ing ♡ Swift!!!" ) // silviu = { firstName: "Silviu", lastName: "Pop", bio: "I f**ing ♡ Swift!!!"}

Testing for identity

Identity Operators Because classes are reference types, it is possible for multiple constants and variables to refer to the same single instance of a class behind the scenes. (The same is not true for structures and enumerations, because they are value types and are always copied when they are assigned to a constant or variable, or passed to a function.) It can sometimes be useful to find out if two constants or variables refer to exactly the same instance of a class. To enable this, Swift provides two identity operators: Identical to (===)

Not identical to (!==)

from the Apple Docs

Let’s assume that we have a list of Users that are in a game and a special User from it is the host. We would like to add some logic only for the users that are not the host. We can do this with the identity operator ===

var users : User [] = [ ... ] // User[] means Array of Users var host = /* some user */ for user in users { if user === host { // host logic here println ( "this is the host" ) } else { // guest logic here println ( "this is a guest" ) } }

Thankfully the equal to == operator does’t work by default, so you don’t have to worry about these kind of bugs.

In part 2 we will cover inheritance and protocols. And this is a complete guide to Object Oriented Programming in Swift.

If you found this usefull please take a moment and share it with your friends.