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 "project" directory.
cd ~/projects mkdir project-1412 cd project-1412
Edit a new file named "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.
Helpful hint: The async-shell-command can be executed with Alt-Shift-7. The command to execute each time is: swift main.swift
- 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.