从班级中的枚举访问var

发布于 2025-01-25 23:13:53 字数 769 浏览 1 评论 0原文

我遵循了代码,但是无法从ENUM访问应用程序的应用程序

open class Foo {
    private var app: Bar  
    enum Directions: String {
        case north = "NorthId"
        case south = "SouthId"
        case east = "EastId"
        case west = "WestId"
        case northEast = "NorthEastId"
        case northWest = "NorthWestId"
        case southEast = "SouthEastId"
        case southWest = "SouthWestId"
        var dir: String {
            switch self {
            case .north, .south, .east, .west 
              return app.getDirection[self.rawValue]
            case northEast, northWest, southEast, southWest
              return app.getCoordinates[self.rawValue]
            }
        }
    }
}

是在枚举指示内无法访问应用程序。如何在另一类变量枚举方向内访问类变量应用程序。

感谢您的帮助

I've following code but unable to access app from Enum

open class Foo {
    private var app: Bar  
    enum Directions: String {
        case north = "NorthId"
        case south = "SouthId"
        case east = "EastId"
        case west = "WestId"
        case northEast = "NorthEastId"
        case northWest = "NorthWestId"
        case southEast = "SouthEastId"
        case southWest = "SouthWestId"
        var dir: String {
            switch self {
            case .north, .south, .east, .west 
              return app.getDirection[self.rawValue]
            case northEast, northWest, southEast, southWest
              return app.getCoordinates[self.rawValue]
            }
        }
    }
}

Here issue is app is not accessible inside enum Directions. How can I access class variable app inside the another class variable Enum Directions.

Thank you for your help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以可爱出名 2025-02-01 23:13:53

我如何在另一个类变量枚举方向内访问类变量应用。

这个问题有一个错误。 应用程序 不是类变量。这是一个属于类foo的实例变量。也就是说,有一个值app每个对象的foo

然后,这就提出了一个问题,如果Directions枚举正在尝试访问app值,它应该使用哪个?

换句话说,名称是人类的实例变量。问“这个人的名字是什么?”是有意义的。但是,问“人的名字是什么?” ...好吧……哪个人是没有意义的?但是,这正是您要在Direction枚举中要做的事情。

很难建议解决方案,而没有更多关于您要实现的目标的上下文,但是我可以自信地说,Direction enum这是不正确的(这只是一个简单的小型类型,描述了8个红衣主教方向)完全可以做任何涉及“应用程序”或任何其他此类高级概念的事情。

How can I access class variable app inside the another class variable Enum Directions.

There's a mistake in this question. app isn't a class variable. It's an instance variable that belongs to instances of the class Foo. That is, there's one value app per object of Foo.

This then begs the question, if the Directions enum was trying to access an app value, which one should it use?

Put another way, a name is an instance variables of humans. It makes sense to ask "What is the name of this person?". However, it doesn't make sense to ask "What is the name of Person?"... well... which person? Yet, this is exactly what you're trying to do in Directions enum.

It's hard recommend a solution without more context about what you're trying to achieve, but I can confidently say that it would be incorrect for the Directions enum (which is just a simple little type that describes 8 cardinal directions) to do anything at all involving an "app", or any other such high-level concepts.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文