barndog
barndog

Reputation: 7173

Swift Nested Structure Access

I have the following nested structure:

public struct Session {
    public enum Type: Int {
        FirstLaunch = 0, NotRegistered, LoggedOut, LoggedIn
    }
}

It's very simple, very barebones. However when I try to access FirstLaunch for example, Xcode throws the following error:

'Session.Type.Type' does not have a member named 'FirstLaunch'

Anyone have any idea what's going on?

Upvotes: 1

Views: 604

Answers (2)

Adolfo
Adolfo

Reputation: 6669

Type is a reserved word in Swift. You can escape it by surrounding it with back ticks.

“If you need to give a constant or variable the same name as a reserved Swift keyword, you can do so by surrounding the keyword with back ticks (`) when using it as a name. However, you should avoid using keywords as names unless you have absolutely no choice.”

Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. https://itun.es/us/jEUH0.l

Upvotes: 5

barndog
barndog

Reputation: 7173

I figured it out. Looks like you can't use the word Type to name any structure. It's probably a reserved keyword or something of that nature.

Upvotes: 1

Related Questions