Post

Replies

Boosts

Views

Activity

Reply to Editable hierarchical list in runtime
Removing parent needs an additional test: } else { // It is the parent for (index, parent) in data.enumerated() { if parent.name == item.name { data.remove(at: index) print("remove \(item.name)") } } For completeness, a very basic Add child. I have assumed that children in nil for a child and not nil but may be empty for parent.: struct FileItem: Identifiable { let name: String var children: [FileItem]? // 👈🏻 Will be nil if child at lowest level of hierarchy ; otherwise for parent, should be [] when no child var id: String { name } } struct ContentView: View { @State var data: [FileItem] = [ FileItem(name: "First", children: [FileItem(name: "childF1"), FileItem(name: "childF2")]), FileItem(name: "Second", children: [FileItem(name: "childS1"), FileItem(name: "childS2"), FileItem(name: "childS3")]), FileItem(name: "Third", children: [FileItem(name: "childT1"), FileItem(name: "childT2")]), ] // var body: some View { // List(data, children: \.children, rowContent: { Text($0.name) }) // } var body: some View { List(data, children: \.children) { item in HStack { Text(item.name) // If there are no children, we cannot remove it if item.children != nil { // So it is parent, maybe with no child [] Spacer() Button("Add child") { // let's search its position in data for (index, parent) in data.enumerated() { if parent.name == item.name && parent.children != nil { // double check on nil data[index].children!.append(FileItem(name: "new child")) } } } } if item.children == nil || item.children!.isEmpty { // nil when item is child, empty for parent Spacer() Button("Remove") { // This is a simple implementation if only children, no grandChildren // if grandchildren, need to have a recursive search for the parent for (index, parent) in data.enumerated() { // If it is children if parent.children != nil && !parent.children!.isEmpty { for child in parent.children! { if child.name == item.name { var newChildren = parent.children! newChildren.removeAll(where: { $0.name == item.name }) data[index].children = newChildren print("remove \(item.name)") } } } else { // It is the parent for (index, parent) in data.enumerated() { if parent.name == item.name { data.remove(at: index) print("remove \(item.name)") } } } } } } } } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’24
Reply to Editable hierarchical list in runtime
Here is a very simple example, just to show how to use hierarchical List struct FileItem: Identifiable { let name: String var children: [FileItem]? var id: String { name } } struct ContentView: View { @State var data: [FileItem] = [FileItem(name: "First", children: [FileItem(name: "child1"), FileItem(name: "child2")])] // State, so that you can modify var body: some View { List(data, children: \.children, rowContent: { Text($0.name) }) } }   In Xcode documentation (searching for List), you will find more details on how to use hierarchical lists: Creating hierarchical lists You can also create a hierarchical list of arbitrary depth by providing tree-structured data and a children parameter that provides a key path to get the child nodes at any level. The following example uses a deeply-nested collection of a custom FileItem type to simulate the contents of a file system. The list created from this data uses collapsing cells to allow the user to navigate the tree structure. struct ContentView: View { struct FileItem: Hashable, Identifiable, CustomStringConvertible { var id: Self { self } var name: String var children: [FileItem]? = nil var description: String { switch children { case nil: return "📄 \(name)" case .some(let children): return children.isEmpty ? "📂 \(name)" : "📁 \(name)" } } } let fileHierarchyData: [FileItem] = [ FileItem(name: "users", children: [FileItem(name: "user1234", children: [FileItem(name: "Photos", children: [FileItem(name: "photo001.jpg"), FileItem(name: "photo002.jpg")]), FileItem(name: "Movies", children: [FileItem(name: "movie001.mp4")]), FileItem(name: "Documents", children: []) ]), FileItem(name: "newuser", children: [FileItem(name: "Documents", children: []) ]) ]), FileItem(name: "private", children: nil) ] var body: some View { List(fileHierarchyData, children: \.children) { item in Text(item.description) } } }    Here is a simple remove implementation: struct ContentView: View { @State var data: [FileItem] = [FileItem(name: "First", children: [FileItem(name: "child1"), FileItem(name: "child2")])] var body: some View { List(data, children: \.children) { item in HStack { Text(item.name) // If there are children, we cannot remove it if item.children == nil || item.children!.isEmpty { Spacer() Button("Remove"){ // This is a simple implementation if only children, no grandChildren // if grandchildren, need to have a recursive search for the parent for (index, parent) in data.enumerated() { // If it is children if parent.children != nil && !parent.children!.isEmpty { for child in parent.children! { if child.name == item.name { var newChildren = parent.children! newChildren.removeAll(where: { $0.name == item.name }) data[index].children = newChildren print("remove \(item.name)") } } } else { // It is the parent for (index, parent) in data.enumerated() { data.remove(at: index) print("remove \(item.name)") } } } } } } } } } Don't forget to close the thread if that's OK. Otherwise, explain where the problem is.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’24
Reply to User Data is getting randomly deleted
Could there be a link with the bug solved in Xcode 15.4 according to release notes:   Resolved Issues Fixed: In certain circumstances, an app can’t read the contents of its own data container after replacing the content of the data container using Xcode or devicectl. (116698465) (FB13253099)
Topic: App & System Services SubTopic: General Tags:
May ’24
Reply to app rejected
You're supposed to provide requested information. Try to imagine reviewer concern: they see a developer speak in the name a company without any official credential. It could well be someone usurping the name of the company to publish an app without their consent. Reviewer cannot guess you have a (verbal) agreement. If the owner is a friend of yours, that should be pretty easy to get the signed document.
May ’24
Reply to SwiftData relationships
In the “Vegetable” class, why is the field “notes” an array of type Notes? You may have several notes, so it is logical to get them in an array. What else did you think about ?   Again in the “Vegetable” class does the field “notes” get stored in the database, if so what is stored? By default, all non-computed attributes are stored. Unless you use the @Transient macro.   In the “Note” Class it looks like the whole of the class “Vegetable” gets stored in the variable “vegetable”, which may or may not get stored in the database. With @Relationship, SwiftData knows what needs to be saved to be able to rebuild the relations when needed. This tutorial should give you some insight.
May ’24
Reply to Swift UI How can I get the click of a button to change the wording of Label
my label should be a state var. And you cannot change with string value, but just reassign a new label: struct ContentView: View { @State private var myLabel = Label("Text to be Changed", systemImage: "circle") var body: some View { Spacer() Button("Change Label Wording"){ myLabel = Label("Changed text", systemImage: "star") } Spacer() myLabel Spacer() } } You could also do it differently. Create a state variable @State private var newLabel = false Toggle in button action: Button("Change Label Wording"){ newLabel.toggle() } Here is a small code snippet to show: struct ContentView: View { @State private var newLabel = false var body: some View { Spacer() Button("Change Label Wording"){ newLabel.toggle() // myLabel.stringValue = "Changed text" } Spacer() Text(newLabel ? "Changed text" : "Text to be Changed") // Or this form Spacer() if newLabel { Text("Changed text (2)") } else { Text("Text to be Changed (2)") } Spacer() } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’24
Reply to FCPXML Creation issue...
Is that just a warning ? Does the file works correctly ? There are many references to similar issues on the web (notably on discussions.apple.com). Did you search ? For instance, this one, where error was due to timecode: https://discussions.apple.com/thread/255188736?sortBy=best
Topic: Media Technologies SubTopic: Video Tags:
May ’24
Reply to Error event - Minimed Mobile App . Triggered by insufficient update interval of the Watch iOS system of only 50 updates/day problem with update interval of the Apple Watch iOS
Welcome to the forum. You have a problem with an existing app ? So this forum is not the right one. you should contact the developer directly. this forum is to be uses for your app, if you have any issue during developpent . So you‘d better close this thread and post it again on minimed forum or send directly to its developer.
May ’24