NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

Multiple Selector Picker

Forums > Swift

So i have been using Realm (a alternative to CoreData) in a Task Managing app, the user has predetermined tasks to choose from, these are in a Picker form so when the user selects the Task he needs via the Picker and clicks the "Add Task" a Task is added.

The problem is that the user can add one task at a time and i want the user to be able to add multiple tasks at once however Picker does not have the ability to select multiple options at once.

I have looked at a lot Multiple Selector Pickers on the internet but they do not return a String value that Realm needs to display the task.

Below is a sample piece of code:

struct AddTaskView: View {

var tasks = ["Study for the exam", "Clean your room"]
@State private var title: String = ""

var body: some View {
        NavigationView {
            Form {
                  Section {
                      Picker("+", selection: $title) {
                          ForEach(tasks, id: \.self) {
                              Text($0)
                          }
                      }
                  } header: {
                    Text("Choose Your Task")
                }
            }
         }
      }
   }

   

I have looked at a lot Multiple Selector Pickers on the internet but they do not return a String value that Realm needs to display the task.

What do they return? It's probably something that you could turn into a String for storing in your Realm db.

   

this is the error it gives me: Cannot convert value of type 'String' to expected argument type 'Set<Goal>'

the 'Set<Goal>' is from this line of code


struct Goal: Hashable, Identifiable {
    var name: String
    var id: String { name }
}

struct Task3 {
    var name: String
    var servingGoals: Set<Goal>
}

   

this is the error it gives me: Cannot convert value of type 'String' to expected argument type 'Set<Goal>'

What gives you that error? What are you trying to do when you get the error? Is this using the standard SwiftUI Picker or one of the multiple selection pickers you say you've tried but don't work with Realm? In order to help you, we need enough information to figure things out.

the 'Set<Goal>' is from this line of code

So given those data structures, you should be able to write code to convert between a Goal and a String. And then you can just add Goals to the Set.

   

for reference the multiselector is from this repository https://github.com/Jeehut/MultiSelectorDemo

What gives you that error?

so basically I created a button that does the task:

if title != "" {
                    realmManager.addTask(courseTitle: title)
                    }

(this is from the actual app where I use a normal picker that has a binding to title when it is clicked the title variable changes and a task is added using that title)

so from these variables:

    @State var task = Task3(name: "", servingGoals: [allGoals[1]])

    @State var isNavigationBarHidden: Bool = true

and the usage of multiselector from the repository here:

MultiSelector(
                        label: Text("Serving Goals"),
                        options: allGoals,
                        optionToString: { $0.name },
                        selected: $task.servingGoals
                    )

I tried to use the selected binding in the if statement rather than the title so it will be like this:

// i get the error here
if task.servingGoals != "" {
                        realmManager.addTask(courseTitle: task.servingGoals)
                    }

What are you trying to do when you get the error?

I tried to use the selected binding in the "if statement" rather than the title

Is this using the standard SwiftUI Picker or one of the multiple selection pickers you say you've tried but don't work with Realm?

It is the multiple selection picker I provided apologies for not including it earlier

So given those data structures, you should be able to write code to convert between a Goal and a String. And then you can just add Goals to the Set.

How do i do that?


I am sorry if I did not make anything clear if you do not understand something feel free to ask me I will explain what I meant.

   

Hacking with Swift is sponsored by MadMachine

SPONSORED Want to explore your Swift skill outside of the Apple world? Join the MadMachine community and start to program microcontrollers in Swift.

Get it now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.