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 trialJivesh Sehgal
742 Pointshow do you use this in swift2 ?
Ive updated my Xcode and now Im more confused than I was before I started this course? Please help
4 Answers
Richard Lu
20,185 PointsHi Jivesh,
From what I understand, the question you're asking is how to print a functions return type in Swift 2.0. The println function in the latest Swift has been shortened down to print. In this videos example, he has
println("Area = "\(calculateArea(10, 2))")
So in Swift 2.0, it should look like this.
print("Area = "\(calculateArea(10, 2))")
There has also been changes to calling a function method (mostly to do with external names), for example:
In Swift 1.2, you could of called a method like this:
func calculateArea(height: Int, width: Int) -> Int {
return height * width
}
calculateArea(100, 2)
Now in Swift 2.0, you could must provide the external parameter names (excluding the first one by default)
calculateArea(100, width: 2)
If the preference was to include/exclude the name, use underscore (_) next to the parameter name. Otherwise you would need to name your external parameters:
Exclude
func calculateArea(height: Int, _ width: Int) -> Int {
return height * width
}
calculateArea(100, 2)
Include
func calculateArea(heightvalue height: Int, widthval width: Int) -> Int { // NOTE: heightval and widthval may also be the same names as the parameters
return height * width
}
calculateArea(heightvalue: 100, widthval: 2)
Everything else in this video should work the same. Hope I've helped. Happy Coding! :)
Jivesh Sehgal
742 PointsThank you Richard Lu .
Emmanuel Darmon
6,115 PointsIf I understand well, in Swift 2 println == print. So now, how do you print to the next line in swift 2 ?
Richard Lu
20,185 PointsHey Emmanuel,
Since println == print, it actually automatically prints the new line. If you really need to add another new line in the same string, you can use the special character '\n' and here's how to go about it.
print("a line\n")
Some more information on that would be located here: http://tinyurl.com/orlz6y8
What you're looking for specifically is under "String Literals".
Emmanuel Darmon
6,115 PointsThanks Richard for your help!
San Francisco
28,373 PointsSan Francisco
28,373 PointsThis seems to still return an error
Richard Lu
20,185 PointsRichard Lu
20,185 PointsHi San Francisco,
I have edited my solution above. In your case I would have the function to look like this:
Happy coding! :)