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.
- Read Initialization (through "Initializer Delegation for Value Types") (Swift Documentation)
- Read Initialization (First paragraph only) (Wikipedia)
- Read Computer Science I Textbook (by Bourke) Chapter 10.1.2. Creating
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:
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.
- 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.