Post

Replies

Boosts

Views

Activity

Reply to Trouble with WWDC21 sample code for CloudKit
To be honest I’ve moved on from CloudKit. There were just too many cases where the paradigm didn’t work for my application. Good luck getting the answers - 7 months on and I still can’t get the out of the box boilerplate code to run correctly without modification. Could be something I’ve got wrong in the old beta configs…
Topic: App & System Services SubTopic: iCloud Tags:
Jan ’22
Reply to Hiding the SideBar when presenting a NavigationLink
Here's some base code (slightly modified boiler plate). At the moment, selecting the item just opens the detail view in the right column. I want the sidebar (left column) to slide away as soon as I select an item. import CoreData struct DetailView: View {     @Environment(\.managedObjectContext) private var viewContext     @ObservedObject var item: Item          func save() {         do {             try viewContext.save()                      } catch {             var nserror  = error as NSError             fatalError("Unresolved error: \(nserror)")         }     }          var body: some View {              Text("Item timestamp: \(item.timestamp!)")         Button(action: {                          item.setElement(data: item.values!, row: 0, col: 2, val: item.getElement(data: item.values!, row: 0, col: 2) + 1)             save()         }) {             Text("Add one")         }     } } struct ContentView: View {     @Environment(\.managedObjectContext) private var viewContext     @FetchRequest(         sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp, ascending: true)],         animation: .default)     private var items: FetchedResults<Item>     var body: some View {         NavigationView {             List {                 ForEach(items) { item in                     NavigationLink(destination: DetailView(item: item)) {                         Text("Item at \(item.timestamp!, formatter: itemFormatter)")                     }                 }                 .onDelete(perform: deleteItems)             }             .toolbar {                 #if os(iOS)                 ToolbarItem(placement: .bottomBar) {                     EditButton()                 }                 #endif                 ToolbarItem(placement: .primaryAction) {                     Button(action: addItem) {                         Label("Add Item", systemImage: "plus")                     }                 }             }         }     }          /*      let value: [Int16] = [1, Int16.max, Int16.min]      let data = Data(fromArray: value)      print(data as NSData) // <0100ff7f 0080>      let roundtrip = data.toArray(type: Int16.self)      print(roundtrip) // [1, 32767, -32768]      */     private func addItem() {         withAnimation {             let newItem = Item(context: viewContext)             newItem.timestamp = Date()             do {                 try viewContext.save()             } catch {                 // Replace this implementation with code to handle the error appropriately.                 // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.                 let nsError = error as NSError                 fatalError("Unresolved error \(nsError), \(nsError.userInfo)")             }         }     }     private func deleteItems(offsets: IndexSet) {         withAnimation {             offsets.map { items[$0] }.forEach(viewContext.delete)             do {                 try viewContext.save()             } catch {                 // Replace this implementation with code to handle the error appropriately.                 // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.                 let nsError = error as NSError                 fatalError("Unresolved error \(nsError), \(nsError.userInfo)")             }         }     } } private let itemFormatter: DateFormatter = {     let formatter = DateFormatter()     formatter.dateStyle = .short     formatter.timeStyle = .medium     return formatter }() struct ContentView_Previews: PreviewProvider {     static var previews: some View {         ContentView().environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)     } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Oct ’21
Reply to Trouble with WWDC21 sample code for CloudKit
To be honest I’ve moved on from CloudKit. There were just too many cases where the paradigm didn’t work for my application. Good luck getting the answers - 7 months on and I still can’t get the out of the box boilerplate code to run correctly without modification. Could be something I’ve got wrong in the old beta configs…
Topic: App & System Services SubTopic: iCloud Tags:
Replies
Boosts
Views
Activity
Jan ’22
Reply to Hiding the SideBar when presenting a NavigationLink
Here's some base code (slightly modified boiler plate). At the moment, selecting the item just opens the detail view in the right column. I want the sidebar (left column) to slide away as soon as I select an item. import CoreData struct DetailView: View {     @Environment(\.managedObjectContext) private var viewContext     @ObservedObject var item: Item          func save() {         do {             try viewContext.save()                      } catch {             var nserror  = error as NSError             fatalError("Unresolved error: \(nserror)")         }     }          var body: some View {              Text("Item timestamp: \(item.timestamp!)")         Button(action: {                          item.setElement(data: item.values!, row: 0, col: 2, val: item.getElement(data: item.values!, row: 0, col: 2) + 1)             save()         }) {             Text("Add one")         }     } } struct ContentView: View {     @Environment(\.managedObjectContext) private var viewContext     @FetchRequest(         sortDescriptors: [NSSortDescriptor(keyPath: \Item.timestamp, ascending: true)],         animation: .default)     private var items: FetchedResults<Item>     var body: some View {         NavigationView {             List {                 ForEach(items) { item in                     NavigationLink(destination: DetailView(item: item)) {                         Text("Item at \(item.timestamp!, formatter: itemFormatter)")                     }                 }                 .onDelete(perform: deleteItems)             }             .toolbar {                 #if os(iOS)                 ToolbarItem(placement: .bottomBar) {                     EditButton()                 }                 #endif                 ToolbarItem(placement: .primaryAction) {                     Button(action: addItem) {                         Label("Add Item", systemImage: "plus")                     }                 }             }         }     }          /*      let value: [Int16] = [1, Int16.max, Int16.min]      let data = Data(fromArray: value)      print(data as NSData) // <0100ff7f 0080>      let roundtrip = data.toArray(type: Int16.self)      print(roundtrip) // [1, 32767, -32768]      */     private func addItem() {         withAnimation {             let newItem = Item(context: viewContext)             newItem.timestamp = Date()             do {                 try viewContext.save()             } catch {                 // Replace this implementation with code to handle the error appropriately.                 // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.                 let nsError = error as NSError                 fatalError("Unresolved error \(nsError), \(nsError.userInfo)")             }         }     }     private func deleteItems(offsets: IndexSet) {         withAnimation {             offsets.map { items[$0] }.forEach(viewContext.delete)             do {                 try viewContext.save()             } catch {                 // Replace this implementation with code to handle the error appropriately.                 // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.                 let nsError = error as NSError                 fatalError("Unresolved error \(nsError), \(nsError.userInfo)")             }         }     } } private let itemFormatter: DateFormatter = {     let formatter = DateFormatter()     formatter.dateStyle = .short     formatter.timeStyle = .medium     return formatter }() struct ContentView_Previews: PreviewProvider {     static var previews: some View {         ContentView().environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)     } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Oct ’21