Cookcademy 1 - Build the Recipe Model

Here are the instructions:

With your new description property above, you can create some small test instances to ensure that it’s working correctly.

let myIngredient = Ingredient(name: "Sugar", quantity: 2.0, unit: Unit.tbs)print(myIngredient.description) // "2 Tablespoons Sugar"

Where do you enter this to get results? In the lesson you are working in a model file…

Just test by typing print(myIngredient) and see if it comes out as expected.

Where do you type in print(myIngredients)? And where does “it come out”? I

When I type it into the recipe.swift file, I just get errors and there is no output screen. Is it supposed to output to the console? Or is there a test screen/feature?

Hi,

It’s hard to know specifically where you are writing this from the original post. That’ll better inform what kind of techniques you can use to test/debug. Are you using text editor, Codecademy environment, xcode?

Can you link to the exercise?

Also as a sanity check try commenting out some code and just printing some normal sentence.

print("hello, world!")

Thanks for your help and quick responses!

I am in Xcode. I am following the lesson Cookcademy 1 Build the Recipe Model.

The directions are to enter all of this in an xcode model file called recipe.Swift in a project called cookcademy. In the project no edits have been made to the contentView files or cookcademyApp.swift file. So anything I type into these files results in errors.

The default contentView.Swift file does print out “Hello World” Because it’s in its default state.

When I type print(“Hello World”) in the recipe file it says “Expressions are not allowed at the top level.”

So far I have not seen lessons on debugging so I don’t know how to do that.

Debugging is just a fancy way for fixing code to make it work.

Take a gander here and see if it helps: xcode - 'Expressions are not allowed at the top level' if the module is not main.swift - Stack Overflow

Ok thanks for the link, but that was not helpful. I am very new to this. I don’t have a main.swift file and I don’t think this is a “command line based project”, but I guess I don’t really know.

I have created and edited exactly one file in this project. When I enter the code it tells me to enter, I get errors. There is no example or sample that they include that works.

I’m not sure what to do next to make this part work.

Hi,

I was having the same issue as the OP. The course article is not clear at all where should we test that code while working in XCode.
However, I was able to muddle forward using the suggestion from @toastedpitabread.

Disclaimer: The way I did this seems convoluted and too much work to test such a small feature, I’m new to iOS programming but not programming in general, so I’m hoping in time I’ll learn a better way to do this.

So, I created a new file called main.swift, and got that test code there:

However, since the Unit enum is inside the Ingredient struct, the provided code won’t work as is, you’ll have to replace Unit.XYZ with Ingredient.Unit.XYZ as in the picture.

After this, if you try to run the code by pressing the play button, XCode complains that we have another main entry point on the code (with good reason, we’re supposedly working on a SwiftUI app, and XCode creates this for us). So we can just comment the @main annotation on the CookcademyApp.swift file:

Screenshot 2021-11-28 at 17.46.59

Finally, if we try to run this code again, the simulator starts (it won’t do anything noticeable) and the messages appear in the debug console (yay!):

NOTE: Don’t forget to undo everything (like deleting the main.swift file and uncommenting the @main annotation), otherwise your app won’t run correctly after this.

P.S. - If someone knows a better way to do this, please just say so, I would really love to know :slight_smile: