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 Enumerations and Optionals in Swift Introduction to Optionals Working with Optional Types

Michael Williams
PLUS
Michael Williams
Courses Plus Student 8,059 Points

problems with enumerations course

This is the second time where I've run into the problem of the code Pasan writes not working in Swift 4. It's either because I missed something suuuuuuper simple, but because it's the second time, I'm guessing it's because this is an old course involving Swift 3, whereas I'm running Xcode v9.0.1.

Here's what Pasan wrote (which works) versus what I wrote (pretty sure it's the same):

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: "Pasa", middleName: "Test", lastName: "Premaratne")
me.fullName()

And here's the error I keep getting as the compiler eternally tries to process everything but to no avail:

Playground execution failed:

error: EnumerationsOptionals.playground:2:5: error: expected expression after operator
    }
    ^
Donald Zarraonandia
Donald Zarraonandia
4,434 Points

I copy and pasted this code into my playground and it ran just fine.

A good reason to not update to the latest version of Xcode

1 Answer