Skip to content

Instantly share code, notes, and snippets.

struct ExpandableButtonItem: Identifiable {
let id = UUID()
let label: String
private(set) var action: (() -> Void)? = nil
}
struct ExpandableButtonPanel: View {
let primaryItem: ExpandableButtonItem
let secondaryItems: [ExpandableButtonItem]
private let noop: () -> Void = {}
private let size: CGFloat = 70
private var cornerRadius: CGFloat {
get { size / 2 }
}
struct ContentView: View {
@State private var showAlert: Bool = false
@State private var alertLabel: String = ""
var body: some View {
NavigationView {
ZStack {
// List
List(1...20, id: \.self) { i in
struct TodoItem: Identifiable {
let id = UUID()
let todo: String
}
// ...
struct TodoItem: Codable, Identifiable {
let id = UUID()
let todo: String
}
struct ContentView: View {
@State private var newTodo = ""
@State private var allTodos: [TodoItem] = []
// ...
}
struct ContentView: View {
@State private var newTodo = ""
@State private var allTodos: [TodoItem] = []
var body: some View {
NavigationView {
VStack {
HStack {
TextField("Add todo...", text: $newTodo)
struct ContentView: View {
@State private var newTodo = ""
@State private var allTodos: [TodoItem] = []
var body: some View {
NavigationView {
VStack {
HStack {
TextField("Add todo...", text: $newTodo)
struct ContentView: View {
// ...
private func saveTodos() {
UserDefaults.standard.set(try? PropertyListEncoder().encode(self.allTodos), forKey: "todosKey")
}
}
struct ContentView: View {
// ...
private func loadTodos() {
if let todosData = UserDefaults.standard.value(forKey: "todosKey") as? Data {
if let todosList = try? PropertyListDecoder().decode(Array<TodoItem>.self, from: todosData) {
self.allTodos = todosList
}
}