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

Hello World Function Swift

Trying to create my own greeting functions that takes a person's name as a parameter. I am declaring the string as optional because if no parameter is given I want it to say hello world. Right now I am unable to get the guard let statement to work so I used an if let, can any show me how to use a guard let in this case?

func greeting(personName: String?) -> Bool { if let unwrappedName = personName { print("Hello " + unwrappedName) return true } else { print("Hello World") return false

}

}

let newGreeting = greeting()
print(newGreeting)

Also I am trying to figure out how to get it to work that if I run the function as "greeting()" it will print "hello world". Right now in order to run this I need to run as greeting(nil) as opposed to greeting(). Any help would be greatly appreciated.

Thanks in advance!!

3 Answers

Let me first display the code that makes this work and then I'll explain why it works.

func greeting(personName: String? = nil) -> Bool {
    guard let unwrappedName = person else {
        print("Hello World")
        return false
    }

    print("Hello \(unwrappedName)")
    return true
}

So first, it is possible to declare the parameter with a default nil value. This makes it possible to call the function without explicitly passing a nil parameter.

Second, when using guard let, you need to declare the else clause when using it. Because you plan to return false if that happens, the code above works, because if personName is nil, you print "Hello World" and return out of the function. If personName is not nil, then you don't hit the else and continue on to print your greeting with the passed in name.

Hope this helps!

Thank you very much that is an extremely helpful reply! 1 more question for you if handling something like this in a real world application (I know this is a stretch for a function this simple) is there a more elegant/efficient solution or would this be an effective way of handling it?

Thanks again!

For something this simple, this should be fine. In a real world application, you're probably likely to do more than just print the name to the debug console. So it would really depend on what exactly you're wanting to do.

As for as guard vs if let, I found a couple of links to various blog posts and stackoverflow answers that offer up different reasons for when you would use one over the other.

OK thanks again!