从班级中的枚举访问var
我遵循了代码,但是无法从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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个问题有一个错误。
应用程序
不是类变量。这是一个属于类foo
的实例变量。也就是说,有一个值app
每个对象的foo
。然后,这就提出了一个问题,如果
Directions
枚举正在尝试访问app
值,它应该使用哪个?换句话说,名称是人类的实例变量。问“这个人的名字是什么?”是有意义的。但是,问“人的名字是什么?” ...好吧……哪个人是没有意义的?但是,这正是您要在
Direction
枚举中要做的事情。很难建议解决方案,而没有更多关于您要实现的目标的上下文,但是我可以自信地说,
Direction
enum这是不正确的(这只是一个简单的小型类型,描述了8个红衣主教方向)完全可以做任何涉及“应用程序”或任何其他此类高级概念的事情。There's a mistake in this question.
app
isn't a class variable. It's an instance variable that belongs to instances of the classFoo
. That is, there's one valueapp
per object ofFoo
.This then begs the question, if the
Directions
enum was trying to access anapp
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.