Post

Replies

Boosts

Views

Activity

Reply to Swift: Can I change views without clicking on a link.
I would try it differently: struct ContentView: View { //#if os(iOS) // var myOS = "iOS" //#elseif os(OSX) // var myOS = "OSX" //#else // var myOS = "Something Else" //#endif struct ScreenIOS: View { var body: some View { VStack { Text("Welcome iOS !") } } } struct ScreenMacOS: View { var body: some View { VStack { Text("Welcome MacOS !") } } } var body: some View { NavigationStack { VStack { Text("PLEASE WAIT …") // Wait do you wait for ? .font(.system(size: 24)) .fontWeight(.bold) } .padding() if ProcessInfo.processInfo.isiOSAppOnMac { // if (myOS == "OSX") { ScreenMacOS() // Goto Screen for iMac } else { ScreenIOS() // go to screen for iOS } } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’24
Reply to Question about textfield
What @darkpaw said, plus: Are you sure you have set the delegate for A, B (and C eventually). I tested your code and it works as is as soon as delegate is set to the ViewController. Note: fields names should start with lowercase.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’24
Reply to IBOutlet becomes nil after ViewController is initialized
Does this crash your app or is it working OK ? Is the IBOutlet declared weak (as it should be) ? Do you get the warning: Instance will be immediately deallocated because property "selectButton" is weak AFAIU, the storyboard adds var it to the view and the view then gets a strong reference to it. Found interesting discussion here: https://stackoverflow.com/questions/51437935/xcode-10-uibutton-custom-deallocated-property-is-weak-in-swift-4 old one: https://forums.swift.org/t/instance-will-be-immediately-deallocated-warning-in-xcode-when-using-weak-property/16596/6
Topic: Programming Languages SubTopic: Swift Tags:
May ’24
Reply to Swift UI TabView selection binding strange behaviour ?
Not an explanation (I am interested to read an "authorised" analysis, but my understanding is that calling onChange on the State var, leads TabView to reevaluate its content. Why ? That is the question ! A possible reason: as oldValue, newValue change, that forces the TabView to redraw (and reevaluate). Generally, I have noticed it is very hard (and hazardous) to try to guess how SwiftUI works to reevaluate views…
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’24
Reply to Is it true that @State is used for Value types and @StateObject is used for Reference types?
That used to be true until recently. With the Observable() it is no more true. From StateObject doc: If you need to store a value type, like a structure, string, or integer, use the State property wrapper instead. Also use State if you need to store a reference type that conforms to the Observable() protocol. To learn more about Observation in SwiftUI, see Managing model data in your app.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’24
Reply to Editable hierarchical list in runtime
I was puzzled by the crash when removing an added item. That had to do with the focus on the TextField… This works more properly: struct FileItem: Identifiable { var 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")]) ] @FocusState private var focusedField: String? var body: some View { List($data, children: \.children) { $item in HStack { TextField("", text: $item.name) // probleme : le clavier disparait à chaque caractère // Avec focused, on passe au dernier TextField .focused($focusedField, equals: item.name) .task { self.focusedField = 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 let childrenCount = parent.children!.count var newChildName = "new child \(childrenCount+1)" for child in parent.children! { // let's not give the same name twice if newChildName == child.name { newChildName = "new child \(childrenCount+100)" } } data[index].children!.append(FileItem(name: newChildName)) } } } .foregroundColor(.green) .buttonStyle(.borderless) } Spacer() Button("Remove") { // This is a simple implementation if only children, no grandChildren // if grandchildren, need to have a recursive search for the parent self.focusedField = nil var deleteDone = false DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { // <<-- To give time to update focus for (index, individual) in data.enumerated() { // remove child if deleteDone { print("I quit loop") ; break } if !deleteDone { if individual.children != nil && !individual.children!.isEmpty { for child in individual.children! { if child.name == item.name { var newChildren = individual.children! newChildren.removeAll(where: { $0.name == item.name }) data[index].children = newChildren deleteDone = true break } } } // remove parent if !deleteDone { for (index, individual) in data.enumerated() { if individual.name == item.name { data.remove(at: index) deleteDone = true break } } } } } } .foregroundColor(.red) .buttonStyle(.borderless) } } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’24
Reply to Editable hierarchical list in runtime
When you add on child line, what do you add ? A child of the parent ? A child of the child ?   I need to have both for each line item. So simple! Remove some tests. Here is an updated code… 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")]), ] @FocusState private var focusedField: String? var body: some View { List($data, children: \.children) { $item in HStack { TextField("", text: $item.name) .focused($focusedField, equals: item.name) // avoid keyboard to disappear at each character .task { self.focusedField = item.name } // 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 print("added") 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")) } } } .buttonStyle(.borderless) } // 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 var deleteDone = false for (index, individual) in data.enumerated() { // remove child if !deleteDone { if individual.children != nil && !individual.children!.isEmpty { for child in individual.children! { if child.name == item.name { var newChildren = individual.children! newChildren.removeAll(where: { $0.name == item.name }) data[index].children = newChildren deleteDone = true break } } } } // REMOVE THIS -> else { // remove parent if !deleteDone { for (index, individual) in data.enumerated() { if individual.name == item.name { data.remove(at: index) deleteDone = true break } } } // } } } .buttonStyle(.borderless) // } } } } } There are still some tuning to be done (error when removing an added child). But that should give you a start point. As I explained, if you want more levels, you can use the same struct and List. But you will have to write the function to search for an item in the hierarchy in order to add or remove it in the data. That's a good exercise.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’24
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 Swift: Can I change views without clicking on a link.
I would try it differently: struct ContentView: View { //#if os(iOS) // var myOS = "iOS" //#elseif os(OSX) // var myOS = "OSX" //#else // var myOS = "Something Else" //#endif struct ScreenIOS: View { var body: some View { VStack { Text("Welcome iOS !") } } } struct ScreenMacOS: View { var body: some View { VStack { Text("Welcome MacOS !") } } } var body: some View { NavigationStack { VStack { Text("PLEASE WAIT …") // Wait do you wait for ? .font(.system(size: 24)) .fontWeight(.bold) } .padding() if ProcessInfo.processInfo.isiOSAppOnMac { // if (myOS == "OSX") { ScreenMacOS() // Goto Screen for iMac } else { ScreenIOS() // go to screen for iOS } } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
May ’24
Reply to SwiftUI How can dertine what device I am running
Just use UIDevice.current.localizedModel var body: some View { HStack { Text("Device Type: ") Text(UIDevice.current.localizedModel) } }
Topic: App & System Services SubTopic: General Tags:
Replies
Boosts
Views
Activity
May ’24
Reply to Clarification Needed on Rejection of Astrology App While Similar Apps Are Approved
You are on the wrong forum. This is not a channel to Apple but a developers' forum where you ask for issues with app development.
Replies
Boosts
Views
Activity
May ’24
Reply to Question about textfield
What @darkpaw said, plus: Are you sure you have set the delegate for A, B (and C eventually). I tested your code and it works as is as soon as delegate is set to the ViewController. Note: fields names should start with lowercase.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
May ’24
Reply to IBOutlet becomes nil after ViewController is initialized
Does this crash your app or is it working OK ? Is the IBOutlet declared weak (as it should be) ? Do you get the warning: Instance will be immediately deallocated because property "selectButton" is weak AFAIU, the storyboard adds var it to the view and the view then gets a strong reference to it. Found interesting discussion here: https://stackoverflow.com/questions/51437935/xcode-10-uibutton-custom-deallocated-property-is-weak-in-swift-4 old one: https://forums.swift.org/t/instance-will-be-immediately-deallocated-warning-in-xcode-when-using-weak-property/16596/6
Topic: Programming Languages SubTopic: Swift Tags:
Replies
Boosts
Views
Activity
May ’24
Reply to Swift UI TabView selection binding strange behaviour ?
Not an explanation (I am interested to read an "authorised" analysis, but my understanding is that calling onChange on the State var, leads TabView to reevaluate its content. Why ? That is the question ! A possible reason: as oldValue, newValue change, that forces the TabView to redraw (and reevaluate). Generally, I have noticed it is very hard (and hazardous) to try to guess how SwiftUI works to reevaluate views…
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
May ’24
Reply to Is it true that @State is used for Value types and @StateObject is used for Reference types?
That used to be true until recently. With the Observable() it is no more true. From StateObject doc: If you need to store a value type, like a structure, string, or integer, use the State property wrapper instead. Also use State if you need to store a reference type that conforms to the Observable() protocol. To learn more about Observation in SwiftUI, see Managing model data in your app.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
May ’24
Reply to Editable hierarchical list in runtime
I was puzzled by the crash when removing an added item. That had to do with the focus on the TextField… This works more properly: struct FileItem: Identifiable { var 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")]) ] @FocusState private var focusedField: String? var body: some View { List($data, children: \.children) { $item in HStack { TextField("", text: $item.name) // probleme : le clavier disparait à chaque caractère // Avec focused, on passe au dernier TextField .focused($focusedField, equals: item.name) .task { self.focusedField = 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 let childrenCount = parent.children!.count var newChildName = "new child \(childrenCount+1)" for child in parent.children! { // let's not give the same name twice if newChildName == child.name { newChildName = "new child \(childrenCount+100)" } } data[index].children!.append(FileItem(name: newChildName)) } } } .foregroundColor(.green) .buttonStyle(.borderless) } Spacer() Button("Remove") { // This is a simple implementation if only children, no grandChildren // if grandchildren, need to have a recursive search for the parent self.focusedField = nil var deleteDone = false DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { // <<-- To give time to update focus for (index, individual) in data.enumerated() { // remove child if deleteDone { print("I quit loop") ; break } if !deleteDone { if individual.children != nil && !individual.children!.isEmpty { for child in individual.children! { if child.name == item.name { var newChildren = individual.children! newChildren.removeAll(where: { $0.name == item.name }) data[index].children = newChildren deleteDone = true break } } } // remove parent if !deleteDone { for (index, individual) in data.enumerated() { if individual.name == item.name { data.remove(at: index) deleteDone = true break } } } } } } .foregroundColor(.red) .buttonStyle(.borderless) } } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
May ’24
Reply to Editable hierarchical list in runtime
When you add on child line, what do you add ? A child of the parent ? A child of the child ?   I need to have both for each line item. So simple! Remove some tests. Here is an updated code… 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")]), ] @FocusState private var focusedField: String? var body: some View { List($data, children: \.children) { $item in HStack { TextField("", text: $item.name) .focused($focusedField, equals: item.name) // avoid keyboard to disappear at each character .task { self.focusedField = item.name } // 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 print("added") 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")) } } } .buttonStyle(.borderless) } // 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 var deleteDone = false for (index, individual) in data.enumerated() { // remove child if !deleteDone { if individual.children != nil && !individual.children!.isEmpty { for child in individual.children! { if child.name == item.name { var newChildren = individual.children! newChildren.removeAll(where: { $0.name == item.name }) data[index].children = newChildren deleteDone = true break } } } } // REMOVE THIS -> else { // remove parent if !deleteDone { for (index, individual) in data.enumerated() { if individual.name == item.name { data.remove(at: index) deleteDone = true break } } } // } } } .buttonStyle(.borderless) // } } } } } There are still some tuning to be done (error when removing an added child). But that should give you a start point. As I explained, if you want more levels, you can use the same struct and List. But you will have to write the function to search for an item in the hierarchy in order to add or remove it in the data. That's a good exercise.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
May ’24
Reply to List or a ForEach from a binding: keyboard issue
Did you get any answer to your FB ? I may have found with focusField Declare @FocusState private var focusedField: String? Then in TextField TextField("", text: $msg) .focused($focusedField, equals: msg) .task { self.focusedField = msg }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
May ’24
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:
Replies
Boosts
Views
Activity
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:
Replies
Boosts
Views
Activity
May ’24
Reply to Can somebody explain why i get this error ? all photos are uploaded. This is a very well know issue
Sometimes upload fails. You have to reload again the specific image. yYou do not show the screenshots section, that would be useful.
Replies
Boosts
Views
Activity
May ’24
Reply to My SwiftUI code is becoming un-SwiftUI-y. I'm looking to make things right again.
If I understand correctly, you could use .onChange modifier to update the content each time user updates one of the settings. If you want more precise answer, please show the part of code where user updates settings.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
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:
Replies
Boosts
Views
Activity
May ’24