Post

Replies

Boosts

Views

Activity

Reply to SwiftData how to delete an entity
This works fine for me. All entities in Group Model are deleted (and all subentities, @RelationShip delete rule is set to .cascade) : private func deleteAll() { print("delete all!") do { if try modelContext.delete(model: Group.self, where: NSPredicate(value: true), includeSubentities: false) { print("OK Groups cleared !") // what to triggers UI update, or @Query ? } } catch { print("error: \(error)") } } But, no trigger of UI/Query update…
Jul ’23
Reply to Failed to build the scheme (When trying to use Canvas
Same problem here. Unable to build standard template in the preview canvas : `ld: Undefined symbols: unsafeMutableAddressor of self #1 : Crash.Item in Crash.Item.timestamp.init : Foundation.Date, referenced from: Crash.Item.timestamp.init : Foundation.Date in Item.o clang: error: linker command failed with exit code 1 (use -v to see invocation) Xcode 15 beta 4 Release Notes : Previewing view code in a SwiftData app results in a linker failure for the canvas despite that code compiling successfully for device and simulator. (111657477) Workaround: Run your app in the simulator to test your UI instead of using Previews.
Jul ’23
Reply to SwiftUI on macOS: Window Management for Commands
1- Given a list of Sujet (Core Data managed objects for example, retrieved with @FetchRequest), in the list view : @State private var selectedSujet: Sujet?     var body: some View {         List(selection: $selectedSujet) {             ForEach(sujets) { sujet in                NavigationLink(destination: SujetDetail(sujet: sujet)) {                   SujetListItem(sujet: sujet)                }            }         } // focused values (Object, action, Set of selection, String, Bool, ...) // Passes the selected object in focusedSceneValue         .focusedSceneValue(\.selectedSujet, $selectedSujet) // Passes the action to the "focus system", executed in this closure when command active in menu         .focusedSceneValue(\.deleteSujetAction) {              if let sujet = selectedSujet {                 print("delete: \(sujet.name)")              }          } } 2- Create an extension for FocusedValues for declarations extension FocusedValues { // Binding of optional selected object     var selectedSujet: Binding<Sujet?>? {         get { self[SelectedSujetKey.self] }         set { self[SelectedSujetKey.self] = newValue}     } // Action declaration     var deleteSujetAction: (() -> Void)? {         get { self[DeleteSujetActionKey.self] }         set { self[DeleteSujetActionKey.self] = newValue }     }     private struct SelectedSujetKey: FocusedValueKey {         typealias Value = Binding<Sujet?>     }     private struct DeleteSujetActionKey: FocusedValueKey {         typealias Value = () -> Void     } } 3- In MenuCommands : struct SujetsMenuCommands: Commands { // Retrieve the focused values     @FocusedValue(\.selectedSujet) var selectedSujet     @FocusedValue(\.deleteSujetAction) var deleteSujetAction     var body: some Commands {         CommandMenu("Sujets") {             Button { deleteSujetAction?() } label: { Text("Delete \(selectedSujetName)") }.disabled(selectedSujet?.wrappedValue == nil || deleteSujetAction == nil ) } } // private var for comfort     private var selectedSujetName: String {         if let sujet = selectedSujet?.wrappedValue {             return sujet.wrappedName         } else {             return "This Sujet"         }     } } If multiple selection:    // 1- @State private var selectedDonnees = Set<Donnee.ID>()    .focusedSceneValue(\.selectedDonnees, $selectedDonnees) // 2-     var selectedDonnees: Binding<Set<Donnee.ID>>? {         get { self[FocusedDonneesSelectionKey.self] }         set { self[FocusedDonneesSelectionKey.self] = newValue }     }     private struct FocusedDonneesSelectionKey: FocusedValueKey {         typealias Value = Binding<Set<Donnee.ID>>     } // 3-     @FocusedBinding(\.selectedDonnees) private var selectedDonnees: Set<Donnee.ID>? In macOS, in multi-windows app, each with its own scene, MenuCommands act accordingly with only focused scene. I don't have tested on iPadOS. MacOS 12 - Xcode 13 beta 5
Sep ’21
Reply to Using Core Data in a new to macOS 12 (beta) `Table`
Try this: Table(selection: $selectedAction, sortOrder: $sortOrder) {             TableColumn("Ref", value: \.reference) { action in Text(action.reference ?? "NO REF") }             TableColumn("Date", value: \.dateCommences) { action in Text(action.dateCommences?.formatted(date: .abbreviated, time: .omitted) ?? "NO DATE") } } rows: {             ForEach(eventActions) { action in                 TableRow(action)            } } }
Sep ’21
Reply to How to use `@FocusedBinding` with optionals?
Try struct FocusedDeckKey : FocusedValueKey {    typealias Value = Binding<DeckViewModel?> } extension FocusedValues {    var currentDeck: Binding<DeckViewModel?>? {        get { self[FocusedDeckKey.self] }        set { self[FocusedDeckKey.self] = newValue }    } } and struct AppCommands: Commands {    @FocusedBinding(\.currentDeck) private var currentDeck }
Jul ’21
Reply to @SectionedFetchRequest using relationship as sectionIdentifier
A workaround to get the whole managedObject, not only one of her string attributes : @SectionedFetchRequest(         sectionIdentifier: \Sujet.type?.objectID,         sortDescriptors: [             SortDescriptor(\Sujet.type?.nom, order: .forward),             SortDescriptor(\Sujet.nom, order: .forward)         ])     var sujets: SectionedFetchResults<NSManagedObjectID?, Sujet> and List(selection: $selectedSujet) {             ForEach(sujets) { section in                 Section(header: HStack {                     let type = context.object(with: section.id!) as! Type                     Label(title: { Text(type.wrappedNom) },                           icon: { type.wrappedIcon                             .resizable()                             .scaledToFit()                             .frame(width: 16, height: 16)                     })                 }) {                     ForEach(section) { sujet in                         NavigationLink(destination: SujetDetail(sujet: sujet, selectedSujet: $selectedSujet), tag: sujet, selection: $selectedSujet) {                             SujetListItem(sujet: sujet)                         }                     }                 }             }         } I can't find possible to get the managedObject from 'section.id', which is a '_NSCoreDataTaggedObjectID', if sectionIdentifier is passed a 'Type?'
Jun ’21
Reply to @SectionedFetchRequest using relationship as sectionIdentifier
Coredata entities : Type (with attribute: "nom") who own many Sujet (with attribute: "nom") (one-to-many relationship) This works for me : List of Sujets, sectioned by Types struct SujetsSectionedListByType: View {     // MARK: - Properties     @SectionedFetchRequest(         sectionIdentifier: \Sujet.type?.nom,         sortDescriptors: [             SortDescriptor(\Sujet.type?.nom, order: .forward),             SortDescriptor(\Sujet.nom, order: .forward)         ])     private var sujets: SectionedFetchResults<String?, Sujet>     @Binding var selectedSujet: Sujet?     // MARK: - Body     var body: some View {         List(selection: $selectedSujet) {             ForEach(sujets) { section in                 Section(header: Text(section.id ?? "")) {                     ForEach(section) { sujet in                         NavigationLink(destination: SujetDetail(sujet: sujet, selectedSujet: $selectedSujet), tag: sujet, selection: $selectedSujet) {                             SujetListItem(sujet: sujet)                         }                     }                 }             }         }         .frame(minWidth: ListsMinWidth)     } }
Jun ’21