Memo:
- There is a global function that will create a CountableRange from floating point values.
for i in stride(from: 0.5, through: 15.25, by: 0.3) {
}
Tuple:
- It is nothing more than a grouping of values. You can use it anywhere you can use a type.
- You can use tuples to return multiple values from a function or method
Computed properties:
- The value of a property can be computed rather than stored.
- "set" is optional. You can create a read-only computed property and you don't need to specify the keyword "get".
- Lots of times a "property" is derived from other state. That's why we use computed properties.
Access control:
- internal: this is the default, it means "usable by any object in my app or framework"
- private: this means "only callable from within this object"
- private(set): this means "this property is readable outside, but not settable"
- fileprivate: accessible by any code in this source file
- public: (for framework only)this can be used by objects outside my framework
- open: (for framework only)public and objects outside my framework can subclass this.
Extensions:
- You can add methods/properties to a class/struct/enum even if you don't have the source.
- The properties you add have no storage associated with them (computed only)
- You can't re-implement methods or properties that are already there (only add new ones)
Enum:
- It can only have discrete states
- An enum is a value type, so it is copied when it is passed around.
- Each state can (but doesn't have to) have its own "associated data".
- When you set the value of an enum you must provide the associated data (if any).
- Swift can infer the type on one side of the assignment or the other (but not both).
- An enum's state is checked with a switch statement.
- If you don't want to do anything in a given case, use "break".
- You must handle all possible cases (although you can default uninteresting cases).
- Each case in a switch can be multiple lines and does not fall through the next case.
- Associated data is accessed through a switch statement using "let syntax"
- An enum can have methods and computed properties but no stored properties
- "mutating" keyword is required because enum is a value type.
ARC (Automatic Reference Counting):
- Reference types (classes) are stored in the heap. The system "counts reference" to each of them and when there are zero references, they get tossed automatically. This process is known as "Automatic Reference Counting" and it is not garbage collection.
- You can influence ARC by how you declare a reference-type var with these keywords: strong, weak, unowned
- strong: normal reference counting
- weak: weak means "if no one else is interested in this, then neither am I, set me to nil in that case". We usually use weak in IBOutlet and delegate.
- unowned: "don't reference count this. Crash if I am wrong". This is very rarely used. Usually only to break memory cycles between objects.