|
It has been very exciting to get started with SwiftUI, make an actual app and run it in the simulator! I think I did OK and I solved the three challenges without much trouble. However, I'm completely lost with the extra challenge, that is storing the currency formatter inside a property. Any hints on how I should approach this? Or is there a certain topic I should revisit to get closer to the answer? |
|
SPOILERS Ahead! This is a four part answer. Each part should take you closer to a solution. Read one at a time and see if you can solve on your own. Or return for more hints. Part I You should be quite comfortable with basic properties such as Strings and Integers, yes?
You're assigning a value to a name. This way, in your code whenever you need a user's last name you use the variable "lastName" rather than hand typing it each time. There's a danger that 99 times you will type "Dolphins", but late one night you will type "D⚬lphins" instead. So @twostraw's lesson seems to be: Review your code and Remove redundancies. Remove the parts of your code that are pure duplicates and are used elsewhere. Here's the example:
He asks the question Can you make a new property to store the currency formatter? The goal then is to create a property, let's call it dollarFormatter, so you can re-use it everywhere in your Tip calculator where you want to format a Double value to look like currency.
So you might ask yourself, "Self? What is format? Is it a String? Is it an Integer?" How do you as a Day 18 coder find out this answer? This is an IMPORTANT skill for you to have! |
|
Part II Move your mouse over the .currency key word in the original code and hold down the option key. The cursor changes to a question mark. ❓ Click the .currency keyword and open the Developer Documentation. This is another skill for you to develop. Learn how to read these signatures!
This tells you that currency is a function that modifies some value, where the value is a The type is complex and named @twostraws gives you this hint. So with this you can at least stub out your new property.
Getting close! |
|
Part III Notice that in the original solution, the format code in Text requires a parameter named code?
You could just hard code the string "USD". But generally this is a bad practice. You should really let your users decide what currency they use, and they will log their preference in their device. So @twostraws shows how to grab the user's preference.
Fine, this is one more step you can put into your new reusable computed property.
|
|
Part IV. Final Step. You've used Apple's documentation to research the pieces. You broke a bigger problem into several smaller steps. And you're almost done. What part is missing? Your goal is to return a format style initialized to be some sort of currency. How do you initialize that Currency struct?
Again, you need to dive into the documentation. Get comfortable with this! Make this skill a GOAL. Let your mouse hover over the word Currency in your code and hold down the option key. When your cursor turns into a ❓click the word and open the documentation. You'll see more information about this struct, scroll down until you find the initializers. Do you see any that use currency codes? Yes? Voila! You can now finish your new computed property.
Now you have a reusable property you can use in your solution. Try it!
|
|
Hi In the extra challenge you were given this
then when you put the dot your will get
Then use that any where require localized currency
|
|
Thanks for the replies to both of you! It seems I was already on the right track yesterday but messed up something in the syntax. I managed to write a crude but working version after Obelix's second hint. And @Obelix yes, I did study the quick help and developer docs, actually I have been checking the official Swift documentation regularly for some time now. :) But as a beginner I often find it hard to understand. It's highly technical and English is not my native language so many of the terms are something I need to look up. And it has been very hard to find an up-to-date dictionary for programming and computer science, between English and my language. Paul's Swift glossary sometimes helps, though. And thanks for revealing how to handle and display SwiftUI stuff in the Playground! Earlier today I was wondering if it's possible. Anyway, I'll play around with this for a while and then I'm off to tackle Challenge 1. |
|
SAVE 50% To celebrate WWDC22, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.
Sponsor Hacking with Swift and reach the world's largest Swift community!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.