Created
September 4, 2021 10:46
-
-
Save bannzai/85c115b6da04e2c74eea5bdc812201eb to your computer and use it in GitHub Desktop.
Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
struct ToDo : Identifiable, Equatable { | |
let id: UUID | |
var title: String | |
var done: Bool | |
} | |
let id = UUID() | |
let todo = ToDo(id: id, title: "value1", done: false) | |
struct ToDoState : Equatable { | |
var todos: [ToDo] = [ | |
todo, | |
.init(id: .init(), title: "value2", done: false) | |
] | |
var completedToDos: [ToDo] { | |
get { | |
return todos.filter { $0.done } | |
} | |
} | |
var unCompletedToDos: [ToDo] { | |
get { | |
return todos.filter { !$0.done } | |
} | |
} | |
} | |
var state = ToDoState() | |
func run() { | |
print("before: ", state) // | |
if let index = state.todos.firstIndex(of: todo) { | |
state.todos[index].done = !state.todos[index].done | |
} | |
print("after: ", state) | |
} | |
run() | |
// before: ToDoState(todos: [__lldb_expr_11.ToDo(id: C46A21CF-32B2-496D-B151-48B3918BF885, title: "value1", done: false), __lldb_expr_11.ToDo(id: 78768BBC-F056-4862-817E-C7140FA4064A, title: "value2", done: false)]) | |
// after: ToDoState(todos: [__lldb_expr_11.ToDo(id: C46A21CF-32B2-496D-B151-48B3918BF885, title: "value1", done: true), __lldb_expr_11.ToDo(id: 78768BBC-F056-4862-817E-C7140FA4064A, title: "value2", done: false)]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment