我在Objective-C到Swift代码更改期间遇到错误。感谢
class func shareLevelUp(toFacebook level: uint, from vc: UIViewController?) {
let report = GymStatusReport.statusReportForLevel(level)
switch report.level {
case 1:
// something
case 2:
// something
case 3:
// something
case 4:
// something
default:
return
}
}
表达模式类型' Int'无法匹配类型' uint的值?' (又名'可选')
答案 0 :(得分:2)
report.level
之类的声音是可选值。你需要打开它,或者你需要你的开关有可选的模式。
展开它看起来像这样:
class func shareLevelUp(toFacebook level: uint, from vc: UIViewController?) {
let report = GymStatusReport.statusReportForLevel(level)
guard let level = report.level else {
return
}
switch level {
case 1:
// something
case 2:
// something
case 3:
// something
case 4:
// something
default:
return
}
}
匹配选项如下所示:
class func shareLevelUp(toFacebook level: uint, from vc: UIViewController?) {
let report = GymStatusReport.statusReportForLevel(level)
switch report.level {
case .some(1):
// something
case .some(2):
// something
case .some(3):
// something
case .some(4):
// something
default:
return
}
}
答案 1 :(得分:0)
使用时将toFacebook level: uint
转换为int:
class func shareLevelUp(toFacebook level: uint, from vc: UIViewController?) {
let report = GymStatusReport.statusReportForLevel(level)
switch Int(report.level) {
case 1:
// something
case 2:
// something
case 3:
// something
case 4:
// something
default:
return
}
}