Need more explanation or document of this syntax please (L5 M5)

This is the first time I see this declaretion:

Anyone can explain to me some points:

  • What _ of _ moduleid:Int meant. Can you give me another example please?

  • What break meant. Can you give me another example please?

1 Like

@lucasjcu

The function declaration includes a parameter that it passed in and that parameter is moduleid which is of type Int

So you have:

func beginModule(moduleId: Int) {  
// Note that the naming convention should be moduleId rather than moduleid

The break key word is used to “break” out of a for loop.

When you loop through data and you have found what you are looking for, there is no point in looping through the remaining data so to exit the loop, the break key word is used.

1 Like

The _ also means that you don’t need the parameter name at the call site

With the parameter name beginModule(moduleId: 5)

Without it

beginModule(5)

When you have a underscore, you don’t need the parameter name, but when you don’t have the underscore you do need the parameter name

If you tried to use beginModule(5) you’d get an error

1 Like

Thanks so much!!!

Thanks bro!!!

Thanks Mikaela. I did forget to explain the purpose of the that underscore. It’s easy to overlook that. I know that Chris Ching often uses the underscore but I always change my version so that the parameter name does appear at the call site merely for clarity. Everyone is different.

2 Likes

One more thing please. In this case there are two functions

why do we declare parameter for the second function beginModule (_ moduleId:Int)
but not for the first function getLocalData()

The getLocalData() requires no parameter since all of the instructions inside the function result in the modules array being populated and the styleData being assigned.

1 Like

Hi everyone,

It appears I have the same issue but I do not think Xcode is recognizing a function or compiler (the range). Please take a look at my screenshot when you get the chance. Thanks!!

Do you see what I mean with the operators? “…<” and “==”

Have you made use of breakpoints in your code to determine if you are getting what you expect?

For example, set a breakpoint at the line

for index in 0..<modules.count {

then run your code.
When Xcode stops at that breakpoint you can interrogate the values in the debug console by typing in commands such as

po modules.count which means Print Out the values of modules.count

the console will print out the number of elements in the array modules

Morning Chris!

Did just that on the line you mentioned. I got a yellow exclamation mark in the ContentView file:

ForEach(0…<model.currentModule!.content.lessons.count) { index in

And the message states “Non-constant range: argument must be an integer literal”

The solution to getting rid of that error is this:

ForEach(0..<model.currentModule!.content.lessons.count, id: \.self) { index in

I don’t quite understand the underlying reason that this is required.

This Swift forums thread discusses it.