Just some notes after reading Apple’s official online guide. Most code are from the guide, with some modification by me.
Modified some code in the following parts. Some better example or fixing in new Xcode.
- Function as parameter (like Comparator)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
- Closure
1 2 3 4 5 6 7 8 9 10 11 |
|
===== Full Notes =====
A Swift Tour
- variable.
1 2 3 |
|
- implicit vs explicit.
1 2 3 |
|
- Values never implicitly converted.
1 2 3 |
|
- Include values in strings.
1 2 3 4 |
|
- Arrays and Dictionaries.
1 2 3 4 5 6 7 8 |
|
Control Flows
- for-in
1 2 3 4 5 6 7 8 9 10 |
|
- optional-value with if
1 2 3 4 5 6 7 8 |
|
- switch
1 2 3 4 5 6 7 8 9 10 11 |
|
- for-in in dictionary
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
- while and do-while
1 2 3 4 5 6 7 8 9 10 11 |
|
- range operators
1 2 3 4 5 6 7 |
|
Functions and Closures
- A classic one
1 2 3 4 |
|
- Use a tuple as return value
1 2 3 4 |
|
- Variable Arguments (varargs)
1 2 3 4 5 6 7 8 9 |
|
- Nested functions
1 2 3 4 5 6 7 8 9 |
|
- Function as return value (like pointer of function)
1 2 3 4 5 6 7 8 |
|
- Function as parameter (like Comparator)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
- Closure
1 2 3 4 5 6 7 8 9 10 11 |
|
Objects and Classes
- A simple class
1 2 3 4 5 6 7 8 9 10 |
|
- Initializer and its opposite
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
- Subclass
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
- Setter and Getter on properties
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
|
- Provide code after before or after setting properties
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
Enumerations and Structures
- Enumerations can have methods
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
- Convert enum between raw values
1 2 3 4 5 6 7 |
|
- Structure is similar to Class, but when structures are always copied when they are passed around, while classes are passed by reference.
1 2 3 4 5 6 7 8 9 |
|
- Structure with associated values.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Protocols and Extendsions
- A classic Protocol.
1 2 3 4 |
|
- Classes, enumerations and structures can all adopt protocols.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
- Extension to add functionality to an existing type.
1 2 3 4 5 6 7 8 9 |
|
- Use Protocol as a variable. (Abstracting)
1 2 3 |
|
Generics
- A classic generic sample.
1 2 3 4 5 6 7 8 9 |
|
- Generic can also be used on functions and methods, as well as classes, enumerations, and structures.
1 2 3 4 5 6 7 |
|
- Add requirements in Generic
1 2 3 4 5 6 7 8 9 10 11 |
|