【发布时间】:2019-11-29 23:24:41
【问题描述】:
在 Apple Language Guide for Swift 5.1 中有一个类型下标的示例。
enum Planet: Int {
case mercury = 1, venus, earth, mars, jupiter, saturn, uranus, neptune
static subscript(n: Int) -> Planet {
return Planet(rawValue: n)!
}
}
let mars = Planet[4]
print(mars)
当我尝试在操场上运行它时,出现编译器错误:
下标不能标记为“静态”
这是 Swift 5.1 特有的新功能吗?
我的 Xcode 是 10.2.1 而不是 11,所以我猜它是。
我还尝试了一个应该与 Swift 5 兼容的在线游乐场,但也出现了错误。 http://online.swiftplayground.run/
【问题讨论】:
标签: swift xcode types subscript