|
Started day 13 today and right away I don't recognize what I'm looking at.
Specifically this part:
First, I. have to mention how difficult it is to search on a term like "using" even in the context of swiftui. Ha. Maybe therel's an official documentation site somewhere, like PHP has? I then looked into the past lessons. The closest thing I found was Day 9 and passing functions into functions. But, the format there is:
Is he passing the Cars struct to the function without a parenthesis? I would expect to see
based on what I've learned so far. But, the shorthand stuff and lack of things like semicolons is kind of melting my brain. So, when passing a struct to a function, no parentheses? Edit: Facepalm! I just remembered that structs are a way to create Types, right? So referring to our own struct names is just like referring to Int or String. But, still please let me know if I got that right. |
|
So your are just saying your function will have a parameter that will have to be of type Car , which in your case is a struct, so yes Car is like Int or String , but in this case its your custom type , so yes your are right , the 2 names for parameter are just for our use again ... one to be used internally inside function( when we call function some where we need it , it will use using , you can use any meaningful name if you want
|
|
Mark wonders about function signatures:
You are looking at the signature of a function. Reading signatures is a skill that you must learn, and yes! it will melt your brain for a bit. When you look at a function's signature, read it OUT LOUD until it makes sense to you. In your code below,
See -> Learn to read Function Signatures @twoStraws has a great video that you should also review. See -> Customize Parameter Labels |
|
Firstly learned in the Day 7 : Why does Swift use parameter labels?. You can make the "call" read how you want by using internal and external parameters. 1st with only one parameters
2nd with two parameters
And the last one with external parameter by using a
Now you choose how each call is made and which reads better (mine is with two on this example). However something like might read better.
|
|
Isn't "using" used as a reserved word for passing a function to a function?
So, I don't think it's day 7 stuff. It seems like it was just confusing to see the Cars type passed like that. I wasn't really thinking about it being passed to a function, just wondering why it didn't need parenthsis if it's a parameter. It seems that the syntax for swift is just weird for me. Thanks though |
|
It is the same as this example. The first name given to a function's parameter is for use outside of the function definition. (when you call the function) The second name given to a function's parameter is for use inside of the function definition. (when you are writing what a function does)
In this case, the first parameter only has one name (the second name), as we have used the underscore as the first name. This way, you don't have to use a name at all when calling the function. But we used the name "number" to refer to it inside of the function definition. But the second parameter has two names given, so you would use the first one "by" when you call the function, but we used the second name "divisor" to refer to it inside the function definition. This allows us to call the function like this
The purpose is that you can read it much more naturally this way, and know what it does much easier than...
|
|
If you are confused about You only need to use Parenthesis after a type name when you are creating a new instance of that type by calling an initializer for the struct/class. You can create a new
You can create a new
You can create a new
(With any parameters that the initializer requires between the parenthesis) But after you have created an instance of the type that you are creating, you can pass it to a function by simply using the name of the variable that you stored it in. No parenthesis needed. When you are defining a function, and list |
|
@mark_ -r - This is not some logical stuff, its the way the compiler understands stuff, every thing we write is to satisfy the conditions laid out in the compiler , many of us cannot understand every thing, so just try and move on, its a really small thing ... which will sort itself out in coming days, the frustations you feel, we all go through ... good luck |
SPONSORED Play is the first native iOS design tool created for designers and engineers. You can install Play for iOS and iPad today and sign up to check out the Beta of our macOS app with SwiftUI code export. We're also hiring engineers!
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.