CS193p Notes - Lecture 4: Grid + enum + Optionals
Tags: ios, swift
Published: 2020-07-26 16:36:11

### Grid

```swift
init (_ items: [Item], viewForItem: @escaping (Item) -> ItemView) {
    self.items = items
    self.viewForItem = viewForItem
}
```

- `@escaping` is required because function is not used inside init but used somewhere else.

```swift
extension Array where Element: Identifiable {
  func firstIndex(matching: Element) -> Int {
    for index in 0..<self.count {
      if self[index].id == matching.id {
        return index
      }
    }
    return 0
  }
}
```

- Adds new function to all Arrays where elements are identifiable
- Name of the file `Array+Identifiable` tells that it's identifiable extension to Array

---

### Enum

```swift
enum FastFoodMenuItem {
  case hamburger(numberOfPatties: Int)
  case fries(size: FryOrderSize)
  case drink(String, ounces: Int)
  case cookie
}

enum FryOrderSize {
  case large
  case small
}

let menuItem: FastFoodMenuItem = FastFoodMenuItem.hamburger(patties: 2)
```

- Enums can contain extra data that's just for specific item like fries can be small or large.

### Optional

```swift
enum Optional<T> {
  case none
  case some(T)
}
```

An optional is an enum.

```swift
var hello: String?              var hello: Optional<String> = .none
var hello: String? = "hello"    var hello: Optional<String> = .some("hello")
var hello: String? = nil        var hello: Optional<String> = .none
```

It's possible to get the value from optional two ways

```swift
hello! // this crashes the program in case the value is none

if let safehello = hello {
  print(safehello)
} else {
  // do something else
}
```

Default

```swift
hello ?? "This is default value"
```

- comma in `if` statement is like sequential `&&` operator meaning that the value that is assigned in first condition can be used in the second condition.

```swift
var indexOfTheOneAndOnlyFaceUpCard: Int? {
  get {
    var faceUpCardIndices = [Int]()
    for index in cards.indices {
      if cards[index].isFaceUp {
        faceUpCardIndices.append(index)
      }
    }
    if faceUpCardIndices.count == 1 {
      return faceUpCardIndices.first
    } else {
      return nill
    }
  }
  set {
    for index in cards.indices {
      if index == newValue {
        cards[index].isFaceUp = true
      } else {
        cards[index].isFaceUp = false
      }
    }
  }
}
```

- When setting new value it turns all the other cards face down
- when getting value it is going to check if there is two face up cards

[@RealLankinen](https://twitter.com/reallankinen)

- [Notes 1](https://dev.to/lankinen/cs193p-notes-lecture-1-course-logistics-and-introduction-to-swiftui-44ap)
- [Notes 2](https://dev.to/lankinen/cs193p-notes-lecture-2-mvvm-and-the-swift-type-system-406f)
- [Notes 3](https://dev.to/lankinen/cs193p-notes-lecture-3-reactive-ui-protocols-layout-5g8a)
- [**Notes 4**](https://dev.to/lankinen/cs193p-notes-lecture-4-grid-enum-optionals-4noj)
- [Notes 5](https://dev.to/lankinen/cs193p-notes-lecture-5-viewbuilder-shape-viewmodifier-fgh)
- [Notes 6](https://dev.to/lankinen/cs193p-notes-lecture-6-animation-1a8f)
- [Notes 7](https://dev.to/lankinen/cs193p-notes-lecture-7-multithreading-emojiart-2ak3)
- [Notes 8](https://dev.to/lankinen/cs193p-notes-lecture-8-gestures-json-1f3m)
- [Notes 9](https://dev.to/lankinen/cs193p-notes-lecture-9-data-flow-26e)
- [Notes 10](https://dev.to/lankinen/cs193p-notes-lecture-10-navigation-textfield-3ei9)
- [Notes 11](https://dev.to/lankinen/cs193p-notes-lecture-11-picker-2hgh)
- [Notes 12](https://dev.to/lankinen/cs193p-notes-lecture-12-core-data-eci)
- [Notes 13](https://dev.to/lankinen/cs193p-notes-lecture-13-persistence-2jeh)
- [Notes 14](https://dev.to/lankinen/cs193p-notes-lecture-14-uikit-integration-1p7m)

[Originally published here](https://www.notion.so/lankinen/CS193p-2020-iOS-development-course-from-Stanford-81c094238e9d471bbf059ea8d83b3e15)