CS193P - Lecture 11: Error Handling Persistence
0:44 error handling
9:59 persistence
- FileManager (filesystem)
- CoreData (SQL database)
- CloudKit (database in the cloud)
- UserDefault (lightweight data)
12:23 File System
- file system like a normal Unix filesystem, it starts at
/
/application
: executable, .jpgs, not writable
/documents
: permanent storage, and always visiable to the user
/support
: permanent storage not seen directly by the user
/caches
: store temporary file
17:30 URL & Data
19:28 Archiving
- 使用
Codable
把物件轉換成 Data
後存擋
- 只要 struct 內的資料都有遵守 codable,那麼該 struct 即有遵守 codable (Swift幫我們做了)
- enum 如果帶有 associate value 的話,要手動處理
- CodingKeys
手動處理:
29:00 UserDefault
String
, Int
, Bool
, Float
, Date
, Data
, Array
or Dictionary
34:53 Demo
概要
- JSON/Codable
- Dealing with thrown errors
- saving out EmojiArt document to the File System
- Implementing the ViewModel for our next MVVM in EmojiArt:
PaletteStore
- UserDefault
點出問題
- 拖拉圖片、拖拉 emoji 之後
- 但 xcode rebuild 又要重頭開始
- 所以我們開始做保存
寫 func save(to url: URL)
- EmojiArtDocument 內的 emojiArt 是我們要保存的資料
- EmojiArtModel 弄出一個
func json() -> Data
,但會發現沒有遵守 Codable
- 原來是 EmojiArtModel.Background 這個 enum 沒有遵守 Codable
- 針對這點,開始實作
init(form decoder: Decode)
& func encode(to encoder: Encoder)
現在關掉重開後還是會發現東西要重來,因為還沒做載入
在 model 的 didSet 方法內新增 scheduleAutosave()
1:23:29 Palette
- 有自己的 MVVM,跟畫布用的不一樣
- store in
userDefault -> HW: iCloud