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:

1 Like

I had the exact same issue and also believe this course article is not clear enough and might use some improvement.

Anyway, I solved the issue by using the ContentView.swift file. I just declared one variable called myIngredient as shown above and then called its value from the Text in the body used to display “Hello World” as default.

Hope this spares some people some time :slight_smile:

P.S. Did anyone have also some issues with the next article? Cookcademy 2 - Build the recipe list… I can’t get the items in the list to open the RecipeDetailView using NavigationLink. I even tried using the download files at the end of the article. Running that code didn’t work as well and I’m starting to think I have some wrong settings.

Hey there! If you run the code in the Canvas panel, it should work. The List isn’t embedded in a NavigationView itself (because its parent view will be in a later article), but it’s embedded in one in the preview. If you want to see it in a simulator, wrap the List in a NavigationView like this:

    var body: some View {
        NavigationView {
            List {
                ForEach(recipes) { recipe in
                    NavigationLink(recipe.mainInformation.name,
                                   destination: RecipeDetailView(recipe: recipe))
                }
                .listRowBackground(listBackgroundColor)
                .foregroundColor(listTextColor)
            }.navigationTitle(navigationTitle)
        }
    }
2 Likes

Thanks! I somehow managed to move forward and found some help in StackOverflow and eventually came up with the same solution :slight_smile:

To be honest, this skill path has some room for improvement. I have been struggling with some of the articles and some of the lessons, but mostly due to lack of information or unclear instructions. Also, I believe it was the protocols and generics lesson that could use some improvement. Some of the tasks won’t let you move forward unless you provide the exact same solution as the master. Sometimes you are asked to declare a variable and the solution uses a constant. Then the only way to move forward is to change from var to let. A couple of times my code compiled just fine, printed the expected result but due to minor differences to the master solution didn’t allow me to move forward.

I sincerely hope you guys can improve this skill path. My experience with the beginner iOS app skill path was totally enjoyable and was really impressed with the whole learning process. This has sadly not been the case with the intermediate skill path and at times has been really frustrating :unamused:

Thanks for the feedback! We’ll take another look at that lesson and fix those errors that you encountered.

1 Like

Thanks for the correction on the Unit struct. (ie Ingredient.Unit.tbs)

I also wish there was a way to compile and run a single file in Xcode for console output/testing purposes… I did go through the debugging lessons and there was no mention of how to do that… If you ever found a way please let me know! @benstone91

*** Another option is navigate to the Xcode project in the terminal and run “swift Recipe.swift” to see the output ***