W1412 Constructors

From Coder Merlin

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 "project" directory.

cd ~/projects
mkdir project-1412
cd project-1412

Edit a new file named "main.swift"

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: M-& (async-shell-command) 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.

Oxygen480-actions-help-hint.svg Helpful hint: The async-shell-command can be executed with Alt-Shift-7. The command to execute each time is: swift main.swift

1

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

let color = Color()

What are the property values after initialization?

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

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:255, green:255, blue:255
red:0, green:255, blue:255
Compile or runtime error
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:0, green:255, blue:255
red:nil, green:nil, blue:nil
red:255, green:255, blue:255
Compile or runtime error

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:0, green:255, blue:255
Compile or runtime error
red:nil, green:nil, blue:nil
red:255, 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:nil, green:nil, blue:nil
Compile or runtime error
red:255, green:255, blue:255
red:0, green:255, blue:255

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
red:255, green:255, blue:255
Compile or runtime error
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?

Compile or runtime error
red:255, green:255, blue:255
red:127, green:127, blue:127
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:0, green:255, blue:255
red:127, green:127, blue:127
red:255, green:255, blue:255

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?

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

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
red:255, green:255, blue:255
Compile or runtime error
red:0, green:255, blue:255


Key Concepts[edit]

  • 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.