Obtaining elements from selected rows in list

Good morning. I can obtain the array elements but cannot obtain the “sub elements”
multiSelection represents the selected array in a list

for element in multiSelection {
                            print(element)
                        }

gives

62675888-D88E-45CD-944D-7CB28FAD9900
D4251779-CB44-4ADD-87D9-C2B3B6BA02E8

with a Xcode message of

  1. Where ‘Element’ = ‘Instrument’, ‘S.Element’ = ‘String.Element’ (aka ‘Character’) (Swift.Array)

I assume the above elements have to resolved to the message but cannot see how to
Thanks

assuming the row elements correspond to the list definition ie id/name/description/inventory /catagory and are encoded .
To push to another list ; Loop/Parse/Decode/Append
Is there an easier method (built into lists ) ? Is there a standard that Swift encodes lists data?
Thanks

Hi Joel,

Reading between the lines in the post above, I assume that you want to copy the selected items to another array.

I’ll go back to the books example I previously showed you and expand on that to copy selected items to another array.

This code makes use of the .onChange(of:....) modifier and as you select additional books from the List, .onChange fires each time and newValue contains the updated list of Id’s selected. There are plenty of comments in the .onChange code so have a look and see if you can understand what is happening.

struct Book: Identifiable, Hashable {
    let id = UUID().uuidString
    let name: String
}

struct ContentView: View {
    @State private var multiSelection = Set<String>()
    @State private var selectedItems = [Book]()

    var books = [
        Book(name: "SwiftUI"),
        Book(name: "Swift"),
        Book(name: "Objective-C"),
        Book(name: "C#"),
        Book(name: "Java"),
        Book(name: "SwiftUI"),
        Book(name: "Swift"),
        Book(name: "Objective-C"),
        Book(name: "C#"),
        Book(name: "Java")
    ]

    var body: some View {
        NavigationStack {
            VStack {
                List(books, selection: $multiSelection) { book in
                    Text(book.name)
                }
                .navigationTitle("Books")
                .listStyle(.plain)
                .toolbar {
                    ToolbarItem(placement: .navigationBarLeading) {
                        if !selectedItems.isEmpty {
                            Button {
                                selectedItems.removeAll()
                            } label: {
                                Image(systemName: "trash")
                            }
                        }

                    }
                    ToolbarItem(placement: .navigationBarTrailing) {
                        EditButton()
                    }
                }
                Text("\(multiSelection.count) selected")

                if !selectedItems.isEmpty {
                    Rectangle()
                        .frame(height: 2)
                    Text("Items you have selected")
                }
                List(selectedItems) { item in
                    Text(item.name)
                }
                .listStyle(.plain)
            }
        }
        .onChange(of: multiSelection) { oldValue, newValue in
            // new value contains all of the id's of the items selected from the books list.
            // so loop through them...
            for value in newValue {
                //  ...and get the associated record from the books array.
                let selectedBook = books.first(where: { $0.id == value })
                // selectedBook is Optional so it must be unwrapped
                if let selectedBook {
                    // if the book is not already in the selectItems array...
                    if !selectedItems.contains(selectedBook) {
                        // ...then add it.
                        selectedItems.append(selectedBook)
                    }
                }
            }
        }
    }
}

#Preview {
    ContentView()
}

Thanks Chris for direction. Much appreciated .
Have a great weekend .
Joel