iOS Functions in Swift Adding Power to Functions Default Values

Alex Abid
Alex Abid
Pro 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
Pro Student 810 Points

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