我注意到某些情况在枚举内部,而不是人们使用 static let 来声明变量的情况。这种做法合理吗?
public enum ExampleEnum {
static let case1 = "case1"
static let case2 = "case2"
static let case3 = "case3"
}
Run Code Online (Sandbox Code Playgroud) 我是Swift的新手,正在尝试一些教程来学习和完善我对Swift的知识.我在这段代码中偶然发现了上面的错误,我不明白.如果你们有人有想法,请在这里解释什么错.
let textChoices = [
ORKTextChoice(text: "Create a ResearchKit app", value:0),
ORKTextChoice(text: "Seek the Holy grail", value:1),
ORKTextChoice(text: "Find a shrubbery", value:2)
]
Run Code Online (Sandbox Code Playgroud)
我通过Xcode提供的建议解决了错误,现在我的代码看起来像
let textChoices = [
ORKTextChoice(text: "Create a ResearchKit app", value:0 as NSCoding & NSCopying & NSObjectProtocol),
ORKTextChoice(text: "Seek the Holy grail", value:1 as NSCoding & NSCopying & NSObjectProtocol),
ORKTextChoice(text: "Find a shrubbery", value:2 as NSCoding & NSCopying & NSObjectProtocol)
]
Run Code Online (Sandbox Code Playgroud)
我从答案中得到了另一种解决方案.虽然它有效,但我仍然不清楚问题和解决方案.我错过了什么概念.