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 Swift Functions and Optionals Functions Function Return Types

Donda Kai
Donda Kai
754 Points

How do i solve this?

hi

returns.swift
func greeting(person: String) {
    return ("Hello \(person)")
}

4 Answers

Hi there,

To make the method return something, you need to add a -> to the declaration. That also states what is being returned, so that's -> String in this case.

Then you need to use the return keyword to make that happen inside the code:

func greeting(person: String) -> String {
    return "Hello \(person)"
}

Hope that makes sense.

Steve.

Donda Kai
Donda Kai
754 Points

That didn't help at all! It's just saying bummer

Pasan Premaratne
Pasan Premaratne
Treehouse Teacher

If you're returning ("Hello (person)"), i.e, you're wrapping the string in parentheses, then you're returning a tuple containing a String, and not a String. Remove the parentheses to return a String.

Works fine for me - have you copied the code exactly? I've just redone the challenge a number of times to check. If you could paste me a screenshot of that code failing the challenge that would be great - there may be something amiss with the system.

Steve.

func greeting(person: String) -> String {
    return "Hello \(person)"
}