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

Day 17: We Split Part two, currencyCode deprecated

Forums > 100 Days of SwiftUI

@vsay  

I got the deprecated warning when try to use Locale.current.currencyCode

Example:

  Text(totalPerPerson, format: .currency(code: Locale.current.currencyCode ?? "USD"))

If I use the suggestion fix Locale.current.identifier then it is non-optinal so "USD" default value not working. When I run the app, it doen't show the $ sign in front of the value.

How do I fix this?

3      

@vsay is currently confounded by currency:

If I use the suggestion fix Locale.current.identifier then it is non-optinal so "USD" default value not working.
When I run the app, it doen't show the $ sign in front of the value.

Playgrounds!

I do not know the latest API for Locale.current. But I used Playgrounds to help me find an answer for you! Let me share with you how. Next time, you might try the power of playgrounds to help.

First, type your original code into Playgrounds. Just as you have it posted above.

// Your code with minor change
let problemTotal = 42.99  // provide a test value
Text(problemTotal, format: .currency(code: Locale.current.currencyCode ?? "USD"))

// Compiler's hint!
'currencyCode' was deprecated in iOS 16: renamed to 'currency.identifier'

Playgrounds reports, indeed, this code has been deprecated. BUT, It also provides a hint from the compiler.
Playgrounds also offers to fix this deprecated code for you. Did you press the Fix button ?

Fix, Part 1

Pressing the Fix button yields the following code, and some more complaints from the compiler.

// Updated code
let problem = Text(problemTotal, format: .currency(code: Locale.current.currency.identifier ?? "USD"))

// More compiler woes:
Value of optional type 'Locale.Currency?' must be unwrapped to refer to member 'identifier' of wrapped base type 'Locale.Currency'

Fix, Part 2

But this also provides a Fix button. I pressed the Fix button a second time and the compiler produced:

// After pressing Fix twice, finally the compiler no longer complains
let problem = Text(problemTotal, format: .currency(code: Locale.current.currency?.identifier ?? "USD"))

So, I think this is telling us that everyone running iOS will have a Locale, and the locale will have a current place. However, some locales may not have a valid currency! (That's why it's optional.)

But if it DOES have a valid currency, you then ask for its identifier and apply this to the value in your Text() object.

Solution

In Playgrounds it's easy to see the results of your test. You just have to build a simple View()struct and ask Playgrounds to display it. If you're not using Playgrounds to conduct simple experiments, I think you're missing out on a powerful tool to help you learn Swift.

import SwiftUI
import PlaygroundSupport

let problemTotal = 42.99
let fixedTotal   = 42.12

// Original code with deprecated currencyCode
let problem = Text(problemTotal, format: .currency(code: Locale.current.currencyCode ?? "USD"))

// Updated code with currency.identifier
let fixed   = Text(fixedTotal, format: .currency(code: Locale.current.currency?.identifier ?? "USD"))

struct CurrencyView: View {
    var body: some View {
        VStack(spacing: 12) {
            problem  // <- This is the view with the deprecated currency
            fixed    // <- This is the updated code.
        }
    }
}

PlaygroundPage.current.setLiveView(CurrencyView() )  // <- This allows you to experiment with views in Playgrounds

7      

@vsay  

@Oblix Thanks for a detail answer. And you have a good point using playground to experiment this.

I did fixed this based on the XCode fix button suggestion, and In XCode project, Locale.current.currency?.identifier is non-optional meaning ?? "USD" is never run, so XCode asked me to remove this. So the final code is something like this:

Text(fixedTotal, format: .currency(code: Locale.current.currency?.identifier))

The problem though when I use XCode simulator and run the app, it doesn't show $ sign in front of the value, so I think either the identifier is empty (not null) or I need to set something in the simulator.

3      

Thanks it helps a lot

3      

It seems like you're encountering a warning related to the usage of Locale.current.currencyCode and the default value not working as expected. To resolve this, you can try the following:

swift Copy code Text(totalPerPerson, format: .currency(code: Locale.current.currencyCode ?? "")) By providing an empty string as the default value, it should correctly handle the currency code and display the $ sign as expected.

3      

Also, I'm not able to edit the money value in the simulator or the preview after following this solution. What's the fix for this?

3      

@rsburr is trying to edit a non-editable view.

Also, I'm not able to edit the money value in the simulator or the preview after following this solution.

This solution is for display purposes and uses a Text()view. Text() views are display only. There is another view for entering and editing text. Perhaps you can start a new thread and post some of your code with comments where you think you're having issues and what solutions you've tried?

3      

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!

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.