Cannot access enum case's rawvalue defined in a global constants file

Viewed 804

I have a global constants file: Constants.swift in an iOS app project. Xcode version is 11.1.

The code in this file:

import Foundation

struct Constants {

    enum DayOfTheWeekend : Int {
        case Saturday = 1
        case Sunday = 2
    }
}

In a different file in the same iOS app project, I have this code in a func within a class:

let day = Constants.DayOfTheWeekend.Saturday.rawvalue

And I get this error:

Value of type 'Constants.DayOfTheWeekend' has no member 'rawvalue'

If I put enum outside of the struct in the same Constants.swift file, I still get the same error.

When I type "Constants.DayOfTheWeekend.Saturday.", Xcode autocomplete feature suggests only "self" and "hashvalue". There is not any rawvalue option.

Where is my mistake?

1 Answers

The syntax is rawValue. See The Swift Programming Language: Enumerations: Raw Values.

Why do you think Xcode autocomplete doesn't suggest that?

It does:

enter image description here

But sometimes autocomplete gets confused, especially if there are some errors elsewhere in one’s code. It also won’t work if the file with the enumeration hasn’t been saved. And sometimes it just gets sufficiently confused that you have to empty the derived data folder and restart Xcode.

Related