W1412 Constructors

From Coder Merlin
Jump to navigation Jump to search
Louis-Emile Durandelle, The Eiffel Tower - State of the Construction, 1888

Prerequisites[edit]

Research[edit]

Initialization is the process of preparing an instance of a class, structure, or enumeration type for use. It requires that we provide an initial value for each stored property. We may also perform any other additional initialization that we require.

Experiment[edit]

Create a directory within your "Experiences" directory:

john-williams@codermerlin:~$ cd ~/Experiences
john-williams@codermerlin:~/Experiences$ mkdir W1412
john-williams@codermerlin:~/Experiences$ cd W1412

Create a new swift project:

john-williams@codermerlin:~/Experiences/W1412$ swift-init

Edit a new file named main.swift:

john-williams@codermerlin:~/Experiences/W1412$ emacs main.swift

For this project you'll repeatedly edit the same file. It's probably easiest to test this file within emacs by typing: F5 r and observing the results. For each question, be sure to understand the behavior before proceeding to the next question. It will be helpful to record your answers and reasoning for later review.

1

class Color {
    let red : UInt8
    let green : UInt8
    let blue : UInt8
} //

let color = Color()

What are the property values after initialization?

red:nil, green:nil, blue:nil
red:255, green:255, blue:255
Compile or runtime error
red:0, green:0, blue:0

2

class Color {
    let red : UInt8
    let green : UInt8
    let blue : UInt8

    init() {
        red = 255
        green = 255
        blue = 255
    } //
} //

let color = Color()

What are the property values after initialization?

red:nil, green:nil, blue:nil
Compile or runtime error
red:255, green:255, blue:255
red:0, green:0, blue:0

3

class Color {
    let red : UInt8
    let green : UInt8
    let blue : UInt8

    init() {
        red = 255
        green = 255
        blue = 255

        red = 0
    } //
} //

let color = Color()

What are the property values after initialization?

red:0, green:255, blue:255
Compile or runtime error
red:255, green:255, blue:255
red:nil, green:nil, blue:nil

4

class Color {
    var red : UInt8
    var green : UInt8
    var blue : UInt8

    init() {
        red = 255
        green = 255
        blue = 255

        red = 0
    } //
} //

let color = Color()

What are the property values after initialization?

red:255, green:255, blue:255
red:nil, green:nil, blue:nil
Compile or runtime error
red:0, green:255, blue:255

5

class Color {
    let red : UInt8? = nil
    let green : UInt8? = nil
    let blue : UInt8? = nil
} //

let color = Color()

What are the property values after initialization?

red:255, green:255, blue:255
Compile or runtime error
red:nil, green:nil, blue:nil
red:0, green:255, blue:255

6

class Color {
    let red : UInt8? = nil
    let green : UInt8? = nil
    let blue : UInt8? = nil

    init() {
    } //
} //

let color = Color(red:0, green:255, blue:255)

What are the property values after initialization?

red:255, green:255, blue:255
Compile or runtime error
red:0, green:255, blue:255
red:nil, green:nil, blue:nil

7

class Color {
    let red : UInt8?
    let green : UInt8?
    let blue : UInt8?

    init() {
    } //

    init(red:UInt8, green:UInt8, blue:UInt8) {
        self.red = red
        self.green = green
        self.blue = blue
    } //

    init(white:UInt8) {
        self.red = white
        self.green = white
        self.blue = white
    } //
} //

let color = Color(red:0, green:255, blue:255)

What are the property values after initialization?

red:127, green:127, blue:127
Compile or runtime error
red:255, green:255, blue:255
red:0, green:255, blue:255

8

class Color {
    let red : UInt8?
    let green : UInt8?
    let blue : UInt8?

    init(red:UInt8, green:UInt8, blue:UInt8) {
        self.red = red
        self.green = green
        self.blue = blue
    } //

    init(white:UInt8) {
        self.red = white
        self.green = white
        self.blue = white
    } //
} //

let color = Color(red:0, green:255, blue:255)

What are the property values after initialization?

red:127, green:127, blue:127
Compile or runtime error
red:255, green:255, blue:255
red:0, green:255, blue:255

9

class Color {
    let red : UInt8?
    let green : UInt8?
    let blue : UInt8?

    init(red:UInt8, green:UInt8, blue:UInt8) {
        self.red = red
        self.green = green
        self.blue = blue
    } //

    init(white:UInt8) {
        self.red = white
        self.green = white
        self.blue = white
    } //
} //

let color = Color(white:127)

What are the property values after initialization?

Compile or runtime error
red:255, green:255, blue:255
red:0, green:255, blue:255
red:127, green:127, blue:127

10

class Color {
    let red : UInt8?
    let green : UInt8?
    let blue : UInt8?

    init() {
        self.red = 0
        self.green = 255
        self.blue = 255
    } //

    init(red:UInt8, green:UInt8, blue:UInt8) {
        self.red = red
        self.green = green
        self.blue = blue
    } //

    init(white:UInt8) {
        self.red = white
        self.green = white
        self.blue = white
    } //
} //

let color = Color()

What are the property values after initialization?

Compile or runtime error
red:255, green:255, blue:255
red:0, green:255, blue:255
red:127, green:127, blue:127

11

class Color {
    let red : UInt8?
    let green : UInt8?
    let blue : UInt8?

    func init() {
        self.red = 0
        self.green = 255
        self.blue = 255
    } //

    init(red:UInt8, green:UInt8, blue:UInt8) {
        self.red = red
        self.green = green
        self.blue = blue
    } //

    init(white:UInt8) {
        self.red = white
        self.green = white
        self.blue = white
    } //
} //

let color = Color()

What are the property values after initialization?

red:127, green:127, blue:127
Compile or runtime error
red:0, green:255, blue:255
red:255, green:255, blue:255


KeyConceptsIcon.png
Key Concepts
  • Initialization is the process of preparing an instance of a class, structure, or enumeration type for use.
    • Initialization requires that we provide an initial value for each stored property.
  • Initialization is handled by defining initializers.
  • Unlike functions and methods, initializer do not return a value.
  • Initial values for stored properties may be:
    • Set within an initializer, or
    • by setting a default value value when the property is defined
  • The simplest initializer is simply defined as init(), with no parameters. Note that is must be called init.
  • As with other functions and methods, a parameter list may be provided.
  • Because the initializer must be called init, argument labels play a crucial role for specifying the correct initializer.
  • Constant properties may be assigned at any point during initialization, but once assigned, may not be further modified.


Designed with pride in Silicon Valley, CA, USA