表达模式类型&#39; Int&#39;无法匹配类型&#39; uint的值?&#39; (又名&#39;可选<uint32>&#39;)

时间:2018-05-22 19:18:39

标签: ios swift swift4

我在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
        }
}

表达模式类型&#39; Int&#39;无法匹配类型&#39; uint的值?&#39; (又名&#39;可选&#39;)

2 个答案:

答案 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
    }

}