c0dehunter
c0dehunter

Reputation: 6160

Access enum from another file within a static function in Swift

A.swift:

struct ApiRequest {
    static func getUrl() {
        // MyEnum.oneDay ---> Use of undeclared type 'MyEnum'
    }
}

B.swift:

public enum MyEnum: String {
    case oneDay = "1d"
    case sevenDays = "7d"
    case thirtyDays = "30d"
}

How do I access MyEnum.oneDay?

Upvotes: 1

Views: 2451

Answers (1)

PPL
PPL

Reputation: 6565

Let's say you have a class B like this,

class B {
    public enum MyEnum: String {
        case oneDay = "1d"
        case sevenDays = "7d"
        case thirtyDays = "30d"
    }
}

you can access it in class A like this,

class A {
    struct ApiRequest {
        static func getUrl() {
            print(B.MyEnum.oneDay)
        }
    }
}

Upvotes: 2

Related Questions