Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Enumerations and Optionals in Swift Introduction to Optionals Downsides to Using If Let

error: nil is incompatible with return type 'Friend' ???

Why is my Xcode saying this: "error: nil is incompatible with return type 'Friend' "

struct Person {
    let firstName: String
    let middleName: String?
    let lastName: String

    func fullName() -> String {
        if middleName == nil {
            return firstName + " " + lastName
        } else {
            return firstName + " " + middleName! + " " + lastName

let me = Person(firstName: "Linus", middleName: nil, lastName: "Karlsson")

let airportCodes = ["CDG": "Charles De Gaulle"]
let newYorkAirport = airportCodes["JFK"]

// Optional Binding Using If Let

if let newYorkAirport = airportCodes["JFK"] {
} else {
    print("Whoops! That key does not exist!")

let weatherDictionary: [String : [String : String]] = [
    "currently" : ["temperature": "22.3"],
    "daily" : ["temperature": "22.3"],
    "weekly" : ["temperature": "22.3"]

if let dailyWeather = weatherDictionary["daily"] {
    if let highTemp = dailyWeather["temperature"] {

if let dailyWeather = weatherDictionary["daily"], let highTemperature = dailyWeather["temperature"] {

// Downsides to using If Let
struct Friend {
    let name: String
    let age: String
    let address: String?

func new(friendDictionary: [String : String]) -> Friend {
    if let name = friendDictionary["name"], let age = friendDictionary["age"] {
        let address = friendDictionary["address"]
        return Friend(name: name, age: age, address: address)
    } else {
        return nil

1 Answer

Found the answer myself!

I forgot the question mark after "Friend" in

func new(friendDictionary: [String : String]) -> Friend {

This is how it should be

func new(friendDictionary: [String : String]) -> Friend? {