|
I got the deprecated warning when try to use Locale.current.currencyCode Example:
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? |
|
@vsay is currently confounded by currency:
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.
Playgrounds reports, indeed, this code has been deprecated. BUT, It also provides a hint from the compiler. Fix, Part 1Pressing the
Fix, Part 2But this also provides a
So, I think this is telling us that everyone running iOS will have a But if it DOES have a valid SolutionIn Playgrounds it's easy to see the results of your test. You just have to build a simple
|
|
@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. |
|
|
|
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. |
|
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? |
|
@rsburr is trying to edit a non-editable view.
This solution is for display purposes and uses a |
SPONSORED Alex is the iOS & Mac developer’s ultimate AI assistant. It integrates with Xcode, offering a best-in-class Swift coding agent. Generate modern SwiftUI from images. Fast-apply suggestions from Claude 3.5 Sonnet, o3-mini, and DeepSeek R1. Autofix Swift 6 errors and warnings. And so much more. Start your 7-day free trial today!
Sponsor Hacking with Swift and reach the world's largest Swift community!
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.
Link copied to your pasteboard.