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 Functions in Swift Adding Power to Functions Default Values

Alex Abid
PLUS
Alex Abid
Courses Plus Student 810 Points

Default Values in Swift

Hi Community!

I am following along with Pasan's video and noticing that when he writes out his code, his xcode is returning values to him.

I have followed his code exactly as presented(I think) but am not getting any values returned on the side of my Xcode? What am I or my Xcode doing wrong?

Here is my code:

// Default values

func carpetCost(havingArea area: Int, carpetColor color: String) -> Int { // Gray carpet - $1/sq ft // Tan carpet $2/sq ft // Deep Blue carpet - $4/sq ft

var price = 0

switch color {
    case "gray": price = area * 1
    case "tan" : price = area * 2
    case "blue": price = area * 4
default: price = 0

} return price }

1 Answer

Are you writing your code in an Xcode Project or an Xcode Playground? The Playground is a file used to try out code without having to worry about developing a user interface. It's there that once a line of code is executed by the compiler, the results appear on the right-hand side of the screen.

Also, just looking at your code again - your switch statement is set to evaluate the contents of color. I'm assuming you've declared this somewhere in your code, but have you assigned a value to it before your switch begins?

Moderator Edit: Moved response from Comments section to Answers. So it may be voted on and/or marked as Best Answer.

Alex Abid
Alex Abid
Courses Plus Student 810 Points

I eventually figured this out, sorry for not responding sooner. I appreciate your help in this!