We want to initialize instances of the Circle
class with the radius value. In order to do so, we can take advantage of customized initializers. Initializers aren't methods, but we will write them with syntax that is very similar to the instance methods. They will use the init
keyword to differentiate from instance methods, and Swift will execute them automatically when we create an instance of a given type. Swift runs the code within the initializer before any other code within a class.
We can define an initializer that receives the radius value as an argument and use it to initialize a property with the same name. We can define as many initializers as we want to, and therefore, we can provide many different ways of initializing a class. In this case, we just need one initializer.
The following lines create a Circle
class and define an initializer within the class body. The code file for the sample is included in the swift_3_oop_chapter_02_02
folder:
class Circle { var radius: Double init(radius: Double) { print("I'm initializing a new Circle instance with a radius value of (radius).") self.radius = radius } }
The initializer is declared with the init
keyword. The initializer receives a single argument: radius
. The code within the initializer prints a message on the console indicating that the code is initializing a new Circle
instance with a specific radius value. This way, we will understand when the code within the initializer is executed. As the initializer has an argument, we can call it a parameterized initializer.
Then, the next line assigns the radius the Double
value received as an argument to the radius
class's Double
property. We will use self.radius
to access the radius
property for the instance and radius
to reference the argument. In Swift, the self
keyword provides access to the instance that is created and we want to initialize. The line before the initializer declares the radius
double property. We will dive deep into the proper usage of properties in Chapter 3, Encapsulation of Data with Properties and Subscripts.
The following lines create two instances of the Circle
class named circle1
and circle2
. Note that it is necessary to use the radius
argument label each time we create an instance because we use the previously declared initializer. The initializer specifies radius
as the name of the argument of the Double
type that it requires. When we create an instance, we have to use the same argument name indicated in the initializer declaration, radius
, followed by a colon (:
), and the value we want to pass for the parameter. The first line specifies radius: 25
; therefore, we will pass 25
to the radius parameter. The second line specifies radius: 50
, and therefore, we will pass 50
to the radius
parameter. The code file for the sample is included in the swift_3_oop_chapter_02_03
folder:
var circle1 = Circle(radius: 25) var circle2 = Circle(radius: 50)
After we enter all the lines that declare the class and create the two instances in the Playground, we will see two messages that say, I'm initializing a new Circle instance with a radius value of
, followed by the radius value specified in the call to the initializer of each instance in the Debug area, at the bottom of the Playground window. On the right-hand side, we will see (2 times) displayed for the call to the print
function within the initializer of the Circle
class.
Hover the mouse over (2 times) and click on the Show Result button displayed with a plus sign (+) and located on the right-hand side, as shown in the following screenshot:
By default, the Playground will display the latest value generated with the call to the print
function: I'm initializing a new Circle instance with a radius value of 50.0
. Because the initializer was executed twice (two times), we want the Playground to display the two generated values. Control-click on the rounded rectangle that displays I'm initializing a new Circle instance with a radius value of 50.0
, and select Value History in the context menu, as shown in the next screenshot:
This way, the Playground will display all the values that were generated each time the print
function was called. In this case, the Playground will display two values. On the right-hand side of each line that creates a Circle
instance, we will see Circle displayed, indicating to us the type of the created instance. Hover the mouse over each Circle text and click on the Show Result button displayed with a plus sign (+) and located on the right-hand side. The Playground will display the value for the radius property below each line that creates a Circle instance, as shown in the following screenshot:
The following screenshot shows the results of running the code in the Swift REPL. After the REPL displays the initialization messages, it displays details about the two instances we just created: circle1
and circle2
. The details include the values for the radius
property:
The following lines show the output that the Swift REPL displays after we create the two Circle
instances:
circle1: Circle = { radius = 25 } circle2: Circle = { radius = 50 }
We can read the two lines as follows: circle1
is an instance of Circle
with its radius property set to 25
, and circle2
is an instance of Circle
with its radius
property set to 50
.
The following screenshot shows the results of running the code in the web-based IBM Swift Sandbox:
Each line that creates an instance uses the class name followed by the argument label and the desired value for the radius
class as an argument enclosed in parentheses. Swift automatically assigns the Circle
type for each of the variables (circle1
and circle2
). After we execute the two lines that create the instances of Circle
, we can take a look at the values for circle1.radius
and circle2.radius
in the Playground. We can click on the Quick Look icon (an eye) on the right-hand side, located on the left-hand side of the Show Result icon that has been converted to a Hide Result icon because we are already showing the results. A popup will display the property and its value for the instance. The following screenshot shows the results of inspecting circle1
:
The following line won't allow the Playground to compile the code and will display a build error because the compiler cannot find a parameterless initializer declared in the Circle
class. The specific error message is the following: Missing argument for parameter 'radius' in call
. The subsequent screenshot shows the error icon on the left-hand side of the line that tries to create a Circle
instance and the detailed Playground execution error displayed within the Debug area at the bottom of the window. We will see a similar error message in the Swift REPL and in the Swift Sandbox. The code file for the sample is included in the swift_3_oop_chapter_02_04
folder:
var circleError = Circle()
Remove the previous line that generated an error and enter the following two lines. The code file for the sample is included in the swift_3_oop_chapter_02_05
folder:
print(type(of: circle1)) print(type(of: circle2))
The Playground will display "Circle
" as a result for both the lines because both the variables hold instances of the Circle
class, as shown in the following screenshot. The type
function allows us to retrieve the runtime type as a value for the instance passed for the of
argument. We will see the same output in the Swift REPL and in the Swift Sandbox:
3.144.172.223