CS193p Notes - Lecture 9: Data Flow
Tags: ios, swift
Published: 2020-07-26 16:38:54

Property Wrappers

- Property wrappers are things like `@Publish` that are put before `var`
- A property wrapper actually creates a struct

```swift
@Published var emojiArt: EmojiArt = EmojiArt()

struct Published {
  var wrappedValue: EmojiArt
  var projectedValue: Publisher<EmojiArt, Never> 
}
var _emojiArt: Published = Published(wrappedValue: EmojiArt())
var emojiArt: EmojiArt {
  get { _emojiArt.wrappedValue }
  set { _emojiArt.wrappedValue = newValue }
}
```

- `Publisher<EmojiArt, Never>` is a type that property wrapper decides
    - The point of wrappers is that it's possible to do something when the value is get/set
    - For example when `Published` wrapperValue is changed, it's calling `objectWillChange.send()`
- `$` is binding
- Binding is used a lot and it's very important because it's all about having a single source of the truth

```swift
struct MyView: View {
  @State var myString = "Hello"
  var body: View {
    OtherView(sharedText: $myString)
  }
}
struct OtherView: View {
  @Binding var sharedText: String
  var body: View {
    Text(sharedText)
  }
}
```

- `@EnvironmentObject` is similar to `.fill()` because when it's use in viewModel it's actually added to all of the views under neath the main View where it's called.

### Publishers ([18:17](https://youtu.be/0i152oA3T3s?t=1097))

- `Publisher<Output, Failure>`
    - `Output` is the type of thing this `Publisher` publishes
    - `Failure` is the type of thing it communicates if it fails while trying to publish

### Demo

```swift
import Combine
...
  @Published private var emojiArt: EmojiArt
  private var autosaveCancellable: AnyCancellable?
...
  init() {
...
    autosaveCancellable = $emojiArt.sink { emojiArt in
      print("\(emojiArt.json?.utf8 ?? "nill")")
      UserDefaults.standard.set(emojiArt.json, forKey: EmojiArtDocument.untitled)
    }
...
```

- When sink is stored to a variable, it's going to work as long as this ViewModel exists
    - This way it's auto saving all the changes as long as ViewModel is used

[@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)