简体   繁体   English

从Swift中的静态函数中的另一个文件访问枚举

[英]Access enum from another file within a static function in Swift

A.swift: A.swift:

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

B.swift: B.swift:

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

How do I access MyEnum.oneDay ? 如何访问MyEnum.oneDay

Let's say you have a class B like this, 假设您有一个这样的B级,

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

you can access it in class A like this, 您可以像这样在A类中访问它,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM