TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: WeSplit: Setting dynamic max value to range based on previous selection

Forums > 100 Days of SwiftUI

Hi all,

This is my first post here - hopefully I'm following protocol.

I'm trying to build on the WeSplit project by adding the ability to determine how much money should go on your credit card. For example, if there are 6 people and the user is paying for 3 of them (say, each owes $40), I want the user to see the total for all 3 ($120).

The problem I'm having is being able to dynamically set the range for the "people on your card" picker based on the user's previous selection in the "number of people" picker (since peopleOnCard should always be < numberOfPeople).

When specifying the range inside the people on card picker

Picker("People on your card", selection: $numberOfPeople) { ForEach(2..<($numberOfPeople + 2)) { Text("\($0) people")

I get the following error: Cannot convert value '$numberOfPeople' of type 'Binding<Int>' to expected type 'Int', use wrapped value instead. But removing the $ sets the range to the original value of numberOfPeople, rather than user selection.

Any suggestions on how I can do this?

2      

In the future, when you want to post code in these forums, it is best to wrap your code in triple backticks. (The button just below the escape key on your keyboard.) You can type 3 backticks on a line, press return twice, then type 3 more backticks. Then, paste your code on the line between the two lines of triple backticks.

This is one way that I just found to make this work.

Basically created a variable to store the number of people that the user is paying for...

@State private var payingFor = 1

Then, created a computed property to calculate the possible range of values that could be selected for that variable...

var payingForOptions: Range<Int> {
    1..<numberOfPeople + 3
}

//This uses +3 because our number of people Picker position 0 is already offset by 2 people, and now we want our range to end with that same number of people.  
//But ForEach won't accept a ClosedRange of Integers, so we have to end the range with <

Then, created a Picker for it like this...

Picker("Paying for", selection: $payingFor) {
    ForEach(payingForOptions, id: \.self) {
        Text("\($0) people")
    }
}

2      

That works. Thank you for the solution and formatting advice!

2      

Hacking with Swift is sponsored by String Catalog.

SPONSORED Get accurate app localizations in minutes using AI. Choose your languages & receive translations for 40+ markets!

Localize My App

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.