CS193p class note - 4 More Swift

###### Memo:
1. We have "mutating" keyword only on func. Because for a property, the keyword "let", "var", "get", "set" will let us know whether the property is immutable.
2. Swift is "copy on write". It means it only copies the whole bits when the object is mutating.
3. You can set the default values for an IBOutlet through `didSet`

###### Protocol:
1. A type which is a declaration of functionality only. Protocols are a way to express an API more concisely. So a protocol is simply a collection of method and property declaration.
2. No data storage of any kind
3. What are protocols good for? 
    - Making API more flexible and expressive
    - Blind, structured communication between View and Controller (delegation)
    -  Mandating behavior (e.g. the keys of a Dictionary must be hashable)
    -  Sharing functionality in disparate types(String, Array, CountablRange are all Collections)
    -  Multiple inheritance(of functionality, not data)
4. Three aspects to a protocol
    - the protocol declaration (which properties and methods are inside)
    - a class, struct or enum declaration that makes the claim to implement the protocol
    - the code in said class, struct or enum (or extension) that implements the protocol
5. In Swift, you need to implement all declared methods/properties in the protocol. But in Objective C, it is possible to mark some methods as optional. Any protocol that has optional methods must be marked `@objc`. Any any class that implements an optional protocol must inherit from `NSObject`
6. You must specify whether a property is get only or both get and set
7. Any functions that are expected to mutate the receiver should be marked `mutating` (unless you are going to restrict your protocol to class implementations only with `class` keyword).
8. In a class, inits must be marked `required` (or otherwise a subclass might not conform).
9. You are allowed to add protocol conformance via an extension.
10. Using extension to provide protocol implementation

###### String:
1. String is not indexed by Int, it is indexed by a different type called String.Index.

###### NSAttributedString:
```swift
let attributes: [NSAttributedString.Key: Any] = [.strokeColor: UIColor.orange,
                                                 .strokeWidth: 5.0]
let attributeText = NSAttributedString(string: "Test", attributes: attributes)
someLabel.attributedText = attributeText
```
1. A String with attributes attached to each character
2. It's a class not a struct. It is a completely different data structure than String. To get mutability, you have to use a subclass of it called `NSMutableAttributedString`.

###### Function Types:
- You can declare a variable (or parameter to a method or whatever) to be of type "function".
- Often you want to create the function "on the fly"(rather than already-existing ones). You can do this by using a closure
- Array has a method called map which takes a function as an argument. It applies that function to each element of the Array to create and return a new Array.

```swift
let primes = [2.0, 3.0, 5.0, 7.0, 11.0]
let negativePrimes = primes.map({-$0}) // [-2.0, -3.0, -5.0, -7.0, -11.0]
let invertedPrimes = primes.map(){1.0/$0} // [0.5, 0.333, 0.2, etc]
let primeStrings = primes.map{String($0)} // ["2.0", "3.0", "5.0", "7.0", "11.0"]
```
Note that if the last (or only) argument to a method is a closure, you can put it outside the method's parentheses that contain its arguments and if the closure was the only argument, you can skip the () completely if you want.

- Closures with property initialization: you can also execute a closure to do initialization of a property if you want. This is especially useful with lazy property initialization

```swift
var someProperty: Type = {
    // construct the value of someProperty here
    return <the constructed value>
}()
```
- Closures are regular types, so they can be put in Array, Dictionary, etc. When this happens, they are stored in the heap (i.e. they are reference types). They capture variables they use from the surrounding code into the heap too. Those captured variables need to stay in the heap as long as the closure stays in the heap.