CS193p 2021
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Sharing URL Help
Menu
Options
Versions and GitHub Sync Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
Invitee
Publish Note

Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

Your note will be visible on your profile and discoverable by anyone.
Your note is now live.
This note is visible on your profile and discoverable online.
Everyone on the web can find and read all notes of this public team.
See published notes
Unpublish note
Please check the box to agree to the Community Guidelines.
View profile
Engagement control
Commenting
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
  • Everyone
Suggest edit
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
Emoji Reply
Enable
Import from Dropbox Google Drive Gist Clipboard
   owned this note    owned this note      
Published Linked with GitHub
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
# CS193P - Lecture 4: Memorize Game Logic - 84 mins * Demo code at cs193p.stanford.edu * Architecture * MVVM - use ViewModel to bind the view to model * protocol `Identifiable` * `Struct`'s mutating function * behide protcol `ObserableObject` * type `Enum` ## [02:56](https://youtu.be/oWZOFSYS5GE?t=176) build a ViewModel If we think of a View as just an agent for showing what in the Model through the ViewModel, then more likely, we're gonna pass it to it as an argument. ```swift= // Before struct ContentView: View { var emojis = [ "🛸", "🚛", "✈️", "🚢", "🚓", "⛵️", "🚁", "🚂", "🚃", "🚀", "🏎", "🚲", "🛵", "🚘" ] @State var emojiCount = 10 var body: some View { LazyVGrid(columns: ...) { ForEach(emojis[0..<emojiCount], id: \.self) { emoji in ... } } } } ``` ```swift= // After struct ContentView: View { @ObservedObject var viewModel: EmojiMemoryGame var body: some View { LazyVGrid(columns: ...) { ForEach(viewModel.cards) { card in ... } } } } ``` #### [12:40](https://youtu.be/oWZOFSYS5GE?t=760) identifiable ```swift= // Before struct Card { var isFaceUp: Bool = false var isMatched: Bool = false var content: CardContent } ``` ```swift= // After struct Card: Identifiable { var isFaceUp: Bool = false var isMatched: Bool = false var content: CardContent var id: Int } ``` #### [31:30](https://youtu.be/oWZOFSYS5GE?t=1890) mutating func ```swift= struct MemoryGame<CardContent> where CardContent: Equatable { var cards: Array<Card> mutating func choose(_ card: Card) { ... cards[chosenIndex].isMatched = true } } ``` #### [33:18](https://youtu.be/oWZOFSYS5GE?t=1998) conform `ObservableObject` 讓 ViewModel conform protocol `ObservableObject` ⬅️ can publish somthing changed ```swift= public protocol ObservableObject : AnyObject { /// The type of publisher that emits before the object has changed. associatedtype ObjectWillChangePublisher : Publisher = ObservableObjectPublisher where Self.ObjectWillChangePublisher.Failure == Never /// A publisher that emits before the object has changed. var objectWillChange: Self.ObjectWillChangePublisher { get } } ``` ```swift= // Before class EmojiMemoryGame: ObservableObject { private var model = createMemoryGame() func choose(_ card: Card) { obhectWillChange.send() model.choose(card) } } ``` ```swift= // After class EmojiMemoryGame: ObservableObject { @Published private var model = createMemoryGame() func choose(_ card: Card) { // obhectWillChange.send() // 因為我們已經加上 @Published,所以任何地方任何人更動了 model,都會自動幫我們呼叫 obhectWillChange.send() model.choose(card) } } ``` > SwiftUI will automatically monitor for such changes, and re-invoke the body property of any views that rely on the data. > [(hackingwithswift) What is the @Published property wrapper?](https://www.hackingwithswift.com/quick-start/swiftui/what-is-the-published-property-wrapper) #### [35:49](https://youtu.be/oWZOFSYS5GE?t=2149) @ObservedObject - `@ObservedObject` means that when this says something changed, please rebuild my entire body. - can only be applied to a `var` ➡️ 完成「一旦 *Model* 更新,*View* 自動連動更新」 ## [37:40](https://youtu.be/oWZOFSYS5GE?t=2260) Enum - enums only have discrete states - an enum is a good data structure to represent that because there's no other options. ```swift= enum FastFoodMenuItem { case hamburger, fries, drink, cookie } ``` #### [39:03](https://youtu.be/oWZOFSYS5GE?t=2343) associated data ```swift= enum FastFoodMenuItem { case .hamburger(numberOfPatties: Int) case .fries(size: FryOrderSize) case .drink(String, ounces: Int) case .cookie } ``` #### [41:40](https://youtu.be/oWZOFSYS5GE?t=2500) checking an enum's state ```swift= var menuItem = FastFoodMenuItem.drink("coke", ounces: 32) switch menuItem { case .hamburger(let pattyCount): print("aburger with \(pattyCount) patties!!") case .fries(let size): print("a \(size) order of fries!") case .drink(let brand, let ounces): print("a \(ounces)oz \(brand)") case .cookie: print("a cookie!") } ``` #### [48:02](https://youtu.be/oWZOFSYS5GE?t=2882) CaseIterable Now this enum will have a static var `allCases` that you can iterate over. ```swift= enum FastFoodMenuItem: CaseIterable { ... } for item in FastFoodMenuItem.allCases { ... } ``` #### [51:09](https://youtu.be/oWZOFSYS5GE?t=3069) optional An `Optional` is just an enum. ```swift= enum Optional<T> { case none case some(T) } ``` #### [1:00:10](https://youtu.be/oWZOFSYS5GE?t=3610) optional chaining ```swift= let x: String? = ... let y = x?.foo()?.bar?.z ``` ## [1:02:35](https://youtu.be/oWZOFSYS5GE?t=3755) Higher-Order-Functions 高階函數 `func index(of card:)` 換成 `firstIndex(where: )` ```swift= // Before func index(of card: Card) -> Int { for index in 0..<cards.count { if cards[index].id == card.id { return index } } return 0 // bogus! } ``` ```swift= // After cards.firstIndex(where: { $0.id == card.id }) ``` #### [1:11:04](https://youtu.be/oWZOFSYS5GE?t=4264) > 開始思考如何「讓場上至多兩張卡片是翻開」的遊戲規則 ```swift= mutating func choose(_ card: Card) { if let chosenIndex = cards.firstIndex(where: { $0.id == card.id }), !cards[chosenIndex].isFaceUp, !cards[chosenIndex].isMatched { if let potentialMatchIndex = indexOfTheOneAndOnlyFaceUpCard { if cards[chosenIndex].content == cards[potentialMatchIndex].content { cards[chosenIndex].isMatched = true cards[potentialMatchIndex].isMatched = true } indexOfTheOneAndOnlyFaceUpCard = nil } else { for index in cards.indices { cards[index].isFaceUp = false } indexOfTheOneAndOnlyFaceUpCard = chosenIndex } cards[chosenIndex].isFaceUp.toggle() } print("\(cards)") } ``` #### [1:23:50](https://youtu.be/oWZOFSYS5GE?t=5030) 兩兩對應的卡片被翻開後 UI 沒有更新,卡片又被蓋回去了 → view 那邊去檢查 isMatched 狀態 ```swift= struct CardView: View { let card: MemoryGame<String>.Card var body: some View { let shape = RoundedRectangle(cornerRadius: 20) if card.isFaceUp { shape.fill().foregroundColor(.white) } else if card.isMatched { // 這邊檢查,已經解答的卡片讓他們隱藏 shape.opacity(0) } else { shape.fill() } } } ``` ## [1:24:52](https://youtu.be/oWZOFSYS5GE?t=5092) Recap *Model* R&R - 弄出 *Model* (`MemoryGame<CardContent>`),跟 UI 毫無關係(不需要 import SwiftUI) - *Model* 內有 資料 (`Array<Card>`)、遊戲邏輯 (`func choose(card:)`) - *Model* 是一個「事實」(*Model* is the truth),所有資料都記錄在那邊 - View 僅僅只是反映出資料長什麼樣子 (reflect the current state of the *Model*) - 把 @State 拿掉了,因為不想把「狀態」這筆資料存在 *View* 裡面 (don't store state in our *Views*. We store them in the *Model*) *ViewModel* R&R - *ViewModel* (`EmojiMemoryGame`) 充當畫面資料提供的轉譯者 - The *ViewModel* also enables the entire reactive architecture. Binding *View* & *ViewModel* - 注意: *Model* 需要是一個 struct 型別,Swift 才能偵測到東西被改變了 - conform ObservableObject 的物件,透過 `objectWillChange.send()` 可以讓整個世界知道被改變了 - 使用 @Published,讓「通知改變」這件事自動發生 - 接著,加上 @ObservedObject,SwiftUI 就會知道當資料更新時需要重新產生 *View* (get its body rebuilt) *View* R&R - *View* 透過 intent functions 來傳遞 UI 事件 --- [Programming Assignment 2](https://cs193p.sites.stanford.edu/sites/g/files/sbiybj16636/files/media/file/Assignment%202.pdf)

Import from clipboard

Paste your markdown or webpage here...

Advanced permission required

Your current role can only read. Ask the system administrator to acquire write and comment permission.

This team is disabled

Sorry, this team is disabled. You can't edit this note.

This note is locked

Sorry, only owner can edit this note.

Reach the limit

Sorry, you've reached the max length this note can be.
Please reduce the content or divide it to more notes, thank you!

Import from Gist

Import from Snippet

or

Export to Snippet

Are you sure?

Do you really want to delete this note?
All users will lose their connection.

Create a note from template

Create a note from template

Oops...
This template has been removed or transferred.
Upgrade
All
  • All
  • Team
No template.

Create a template

Upgrade

Delete template

Do you really want to delete this template?
Turn this template into a regular note and keep its content, versions, and comments.

This page need refresh

You have an incompatible client version.
Refresh to update.
New version available!
See releases notes here
Refresh to enjoy new features.
Your user state has changed.
Refresh to load new user state.

Sign in

Forgot password

or

By clicking below, you agree to our terms of service.

Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
Wallet ( )
Connect another wallet

New to HackMD? Sign up

Help

  • English
  • 中文
  • Français
  • Deutsch
  • 日本語
  • Español
  • Català
  • Ελληνικά
  • Português
  • italiano
  • Türkçe
  • Русский
  • Nederlands
  • hrvatski jezik
  • język polski
  • Українська
  • हिन्दी
  • svenska
  • Esperanto
  • dansk

Documents

Help & Tutorial

How to use Book mode

Slide Example

API Docs

Edit in VSCode

Install browser extension

Contacts

Feedback

Discord

Send us email

Resources

Releases

Pricing

Blog

Policy

Terms

Privacy

Cheatsheet

Syntax Example Reference
# Header Header 基本排版
- Unordered List
  • Unordered List
1. Ordered List
  1. Ordered List
- [ ] Todo List
  • Todo List
> Blockquote
Blockquote
**Bold font** Bold font
*Italics font* Italics font
~~Strikethrough~~ Strikethrough
19^th^ 19th
H~2~O H2O
++Inserted text++ Inserted text
==Marked text== Marked text
[link text](https:// "title") Link
![image alt](https:// "title") Image
`Code` Code 在筆記中貼入程式碼
```javascript
var i = 0;
```
var i = 0;
:smile: :smile: Emoji list
{%youtube youtube_id %} Externals
$L^aT_eX$ LaTeX
:::info
This is a alert area.
:::

This is a alert area.

Versions and GitHub Sync
Get Full History Access

  • Edit version name
  • Delete

revision author avatar     named on  

More Less

Note content is identical to the latest version.
Compare
    Choose a version
    No search result
    Version not found
Sign in to link this note to GitHub
Learn more
This note is not linked with GitHub
 

Feedback

Submission failed, please try again

Thanks for your support.

On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

Please give us some advice and help us improve HackMD.

 

Thanks for your feedback

Remove version name

Do you want to remove this version name and description?

Transfer ownership

Transfer to
    Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

      Link with GitHub

      Please authorize HackMD on GitHub
      • Please sign in to GitHub and install the HackMD app on your GitHub repo.
      • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
      Learn more  Sign in to GitHub

      Push the note to GitHub Push to GitHub Pull a file from GitHub

        Authorize again
       

      Choose which file to push to

      Select repo
      Refresh Authorize more repos
      Select branch
      Select file
      Select branch
      Choose version(s) to push
      • Save a new version and push
      • Choose from existing versions
      Include title and tags
      Available push count

      Pull from GitHub

       
      File from GitHub
      File from HackMD

      GitHub Link Settings

      File linked

      Linked by
      File path
      Last synced branch
      Available push count

      Danger Zone

      Unlink
      You will no longer receive notification when GitHub file changes after unlink.

      Syncing

      Push failed

      Push successfully