UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Day 17 - Reading text from the user with TextField

Forums > 100 Days of SwiftUI

TextField("Amount", value: $checkAmount, format: .currency(code: "USD"))

I can't get this code to work, specifically the format parameter doesnt suggest .currency. Have this been superceeded?

Any help would be great, thanks!

3      

Did you add @State private var checkAmount = 0.0 ?

struct ContentView: View {
    @State private var checkAmount = 0.0

    var body: some View {
        TextField("Amount", value: $checkAmount, format: .currency(code: "USD"))
            .keyboardType(.decimalPad)
    }
}

4      

I pasted your code into my HackingWithSwift Playground.

It worked without issue. The code is fine.

At this point, you may need to cast other spells. Clean your build folder. Stop and start XCode. And, of course, verify Vince's point!

Also, the compiler has been known to blame one section of code when the real trouble lies elsewhere. If you still have issues, you may need to show us more. good luck

4      

I am having the same problem. @DiamondJimBob or anyone else, were you able to resolve this?

Getting a couple of errors saying:

  1. Missing argument for parameter 'formatter' in call
  2. Extra argument 'format' in call
  3. Cannot infer contextual base in reference to member 'currency'

2      

What iOS version is your build targeting? That initializer on TextField is available in iOS 15+.

2      

@roosterboy unfortunately, I'm on an older version of Xcode (12.4) and my depoyment target only goes upto 14.4. Do you think that could be the issue? if so, any idea on how things were done before IOS 15?

2      

Do you think that could be the issue?

Yes, that's exactly the issue.

if so, any idea on how things were done before IOS 15?

You would need to construct and use an explicit NumberFormatter set to format as currency.

So the pre-iOS 15 version of this:

struct ContentView: View {
    @State private var checkAmount = 0.0

    var body: some View {
        TextField("Amount", value: $checkAmount, format: .currency(code: "USD"))
            .keyboardType(.decimalPad)
    }
}

would be something like this:

struct TextFormatterView: View {
    @State private var checkAmount = 0.0
    @State private var currencyFormatter: NumberFormatter = {
        let fmt = NumberFormatter()
        fmt.numberStyle = .currency
        return fmt
    }()

    var body: some View {
        TextField("Amount", value: $checkAmount, formatter: currencyFormatter)
            .keyboardType(.decimalPad)
    }
}

4      

Awesome. Thank you for your help.

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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.