Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Xcode which I should never update!

func calculateArea(height: Int, width: Int) { let area = height * width print("The area is(area)") }

calculateArea(12, width: 12)

Why in this code the first argument is not required but the second is? when I remove width it show error!

5 Answers

Emil Rais
Emil Rais
26,875 Points

In Swift the label is by default required for all parameters except the first one in functions. Labels are however required by default for all parameters in constructors (the ones making use of the init keyword).

The fact that for functions it is required for all parameters except the first one comes down to history. In ObjectiveC developers used to encode what the parameters were to contain into the function names.

lengthOfBytesUsingEncoding
stringByReplacingOccurrencesOfStringWithString

This particular function was supposed to receive two parameters, bytes and an encoding. In Swift this same effect can be had using named parameters but it is more clear now as the label shows up next to the parameter instead of right after each other at the end of the function name. I'll give you an example very soon, but notice how the default encourages that the first parameter is still encoded into the function name.

lengthOfBytes(someBytes, using: someEncoding)
stringByReplacingOccurrencesOfString(someString, with: someOtherString)

You can however for your own functions overrule this behavior and you can also change the labels.

externalName internalName: Type

The external name is the label you have to specify when calling the function. If you don't specify an external name it will use the default. If you put _ as external name it means "Don't use a label". Prior to Swift 2.0 you could use # on the left side of the internal name which would duplicate it as external name but that functionality has now been replaced with an error message informing one of the change. Instead one will now have to be explicit about the external name.

func a(height: Int, width: Int) {
}

func b(height: Int, _ width: Int) {
}

func c(tallness height: Int, fatness width: Int) {
}

// No longer works as of Swift 2.0
func d(#height: Int, width: Int) {
}

And you would call them like

a(5, width: 10)  // This is the default
b(5, 10)
c(tallness: 5, fatness: 10)
d(height: 5, width: 10)

Does this answer your question?

Emil Rais

func c(#height: Int, width: Int) {
}

This won't work in Swift 2.0, you have to specify externalParamenter internalParameter: AnyObject

Emil Rais
Emil Rais
26,875 Points

Thank you Stepan, I have revised my answer based on your information.

Thanks Emil,

can you tell me I can I downgrade my Xcode? the it worked before updating? because I'm kinda new to programming and I do not think it's useful for beginners start messing up with all new changed in Xcode. I need first understand the logic of programming.

Thanks in Advance

Emil Rais
Emil Rais
26,875 Points

I don't know if it is possible. Are there any significant changes that trouble you?

I'm taking the course here(From Amit) what he explains in the video now a little bit bit different(By the way the code I wrote was working on the previous Xcode before update, as Amit showed) so I do not like all those changes, I have not covered up everything yet to be a programmer, so it's kinda troubles me

Emil Rais
Emil Rais
26,875 Points

If you need help fixing the errors you can find it here at Team Treehouse. It should be okay - Xcode 7 also has a feature that updates all code from Swift to Swift 2.0. Have you tried seeing if that fixes your errors? Otherwise deal with each individual error on its own. Given a code snippet and an error message I should be able to help you.

By the way, when I open Assistant Editor it's empty

Emil Rais
Emil Rais
26,875 Points

At the top of the assistant editor is a bar. If the first term on the right of the left and right arrows is set to "automatic" it will automatically display the code that corresponds to a ViewController with focus in the Storyboard. If you want two screens of code open at the same time you can use "manual" instead. Then you need to navigate to the file you want to display.

Perhaps the Assistant Editor is empty because you're on an inappropriate setting?

Can not find (((