iOS App Development
The user builds a pizza and a total price updates as they tap. The price function is written for you. You work out what variables it needs, then build the controls that feed it.
Don't type it. It's already in your starter code below. Just read it.
if / else from Lesson 5.var price = 0.0 — money needs decimals, so price is a Double. Every price is written 9.0, not 9, for the same reason.Double(quantity) — casting, from Lesson 1. quantity is an Int because it comes from a Stepper, and Swift won't multiply a Double by an Int.The function uses six variables that don't exist yet, and your receipt needs a seventh. Work out each one before you write any code. The first row is done for you.
| Variable | Type | Starting value | Control | Correct |
|---|
How to tell: if pepperoni { with nothing after it means it's already true or false. size == "Small" compares it to text. Double(quantity) converts it from a whole number.
Write your code here. The checks update as you type.
Every check above passing means your code has the right pieces. It does not mean the app works. Only Playground can tell you that. Copy and paste your code into a new Playground app to make sure it works.
Even if you wrote your code in Playground the whole time, run it again now that everything is connected.
ContentView.swift and paste over it.Replace the whole file. Do not paste underneath what is already there.$.Not working? Paste your code back into the editor above so the checker can look at it. The red notes will tell you what is missing.
Your app has the right pieces, but it could still add up wrong. Set your controls to each row below and check the total against the answer. Tick the box when yours matches.
"small" is not "Small", capital letters included.price = price * Double(quantity) is still there and still comes after the toppings.delivery starts as true. It should start as false.isOn: against the function.0.08| Problem | Check this |
|---|---|
Cannot find '___' in scope | That @State line is missing, or spelled differently |
| The size never changes the price | Your Picker words don't match the function. "small" is not "Small" |
| The total never changes at all | You wrote total instead of total() |
Cannot find 'total' in scope | The function is outside the struct. It goes inside, after body |
| A control shows but does nothing | You forgot the $ |
Cannot convert value of type... | A starting value is the wrong type. Check your Step 2 table |
| Right at 1 pizza, wrong at 3 | Don't retype the function. Compare yours to Step 1 line by line |