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)
}
}
Post
Replies
Boosts
Views
Activity
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?'
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
}
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)
}
}
}
If you add this? :
extension FocusedValues {
// MARK: - Actions
var filterAction: (() -> Void)? {
get { self[FilterActionKey.self] }
set { self[FilterActionKey.self] = newValue }
}
private struct FilterActionKey: FocusedValueKey {
typealias Value = () -> Void
}
}
MacOS 12 - Xcode 13 beta 5
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
The same here for :
kUTTypeFileURL
kUTTypeUTF8PlainText
despite import :
import MobileCoreServices
import UniformTypeIdentifiers
the error :
'kUTTypeFileURL' was deprecated in iOS 15.0: Use UTTypeFileURL instead.
'kUTTypeUTF8PlainText' was deprecated in iOS 15.0: Use UTTypeUTF8PlainText instead.
The same here for :
kUTTypeFileURL
kUTTypeUTF8PlainText
despite import :
import MobileCoreServices
import UniformTypeIdentifiers
the error :
'kUTTypeFileURL' was deprecated in iOS 15.0: Use UTTypeFileURL instead.
'kUTTypeUTF8PlainText' was deprecated in iOS 15.0: Use UTTypeUTF8PlainText instead.
Seem to be :
UTType.fileURL.identifier
UTType.utf8PlainText.identifier
compiler happy :)
import UniformTypeIdentifiers
static var draggableType = UTType.data.identifier
Apple doc :
Type Property
data
A base type that represents any sort of byte stream, including files and in-memory data.
Discussion
The identifier for this type is public.data.
This type conforms to UTTypeItem.
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.
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…
I had the same experience.
Only on macOS, sidebar not mouse responsive, a little bit with keyboard arrows.
On iOS, TabView behaves as expected
Xcode 16.1 beta 2
MacOS Version 15.1 beta (24B5055e)