快速

时间:2017-06-16 12:48:06

标签: ios swift xcode compilation

我有一个范围表,基本上,我想从中找到我的分数。我的表格看起来像enter image description here

我的代码如下所示:

let ageForRhr = [18...25,26...35,36...45,46...55,56...65,65...100]

    let menRhrScore = [ [49...55 , 56...61,62...65,66...69,70...73,74...81,82...200],
                    [49...54 , 55...61,62...65,66...70,71...74,75...81,82...200],
                    [50...56 , 57...62,63...66,67...70,71...75,76...82,83...200],
                    [50...57 , 58...63,64...67,68...71,72...76,77...83,84...200],
                    [51...56 , 57...61,62...67,68...71,72...75,76...81,82...200],
                    [50...55 , 56...61,62...65,66...69,70...73,74...79,80...200]]

    let womenRhrScore = [ [54...60 , 61...65,66...69,70...73,74...78,79...84,85...200],
                          [54...59 , 60...64,65...68,69...72,73...76,77...82,83...200],
                          [54...59 , 60...64,65...69,70...73,74...78,79...84,85...200],
                          [54...60 , 61...65,66...69,70...73,74...77,78...83,84...200],
                          [54...59 , 60...64,65...68,69...73,74...77,78...83,84...200],
                          [54...59 , 60...64,65...68,69...72,73...77,78...84,85...200]]
    func getRhrStatusFromScore (score : Int, age : Int , isMale : Bool) -> String
    {
        for index in ageForRhr.indices
        {
            if  let arr  = ageForRhr[index]
            {
                if arr.contains(age)
                {
                    if let arrVO2 = isMale ? menRhrScore[index] : womenRhrScore[index]
                    {
                        for indSub in arrVO2.indices
                        {
                            if let subArr = arrVO2[indSub]
                            {
                                if subArr.contains(score)
                                {
                                    if let activity = rhrStatus(rawValue: indSub)
                                    {
                                        return activity.getValue()
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return ""
    }
    enum rhrStatus : Int
    {
    case poor           = 6
    case belowAve       = 5
    case average        = 4
    case aboveAve       = 3
    case good           = 2
    case excellent      = 1
    case athlete        = 0
   }

但不幸的是,swift编译器在编译上面的代码时卡住了。 (没有给出任何错误或警告,只是坚持编译swift文件。如果我评论上面的代码它很容易编译。)请建议一个更好的方法。

提前致谢。

2 个答案:

答案 0 :(得分:1)

当您未明确指定文字类型时,Swift编译器无法编译大型数组文字。它超过了Swift的类型推理系统,需要很长时间或永远不会完成。

在这种情况下,似乎有必要为数组本身和各个组件提供显式类型。

这些更改将允许您的数组进行编译:

actorSystem.awaitTermination

您的代码存在编译错误,现在Swift无法解锁,您可以查看并修复这些错误。

答案 1 :(得分:0)

你的代码看起来像是一个巨大的混乱,有这么多不同的数组。像这样定义一个合适的数据模型:

struct HeartRateStatus {
    enum Gender {
        case male
        case female
    }

    enum Status: String {
        case athlete
        case excellent
        case good
        case aboveAverage
        case average
        case belowAverage
        case poor
        case unknown
    }

    var gender: Gender
    var status: Status
    var ageRange: ClosedRange<Int>
    var scoreRange: ClosedRange<Int>
}

然后是查找数组:

let rhrLookup = [
    HeartRateStatus(gender: .male, status: .athlete, ageRange: 18...25, scoreRange: 49...55),
    HeartRateStatus(gender: .male, status: .athlete, ageRange: 26...35, scoreRange: 49...54),
    // ...
    HeartRateStatus(gender: .female, status: .athlete, ageRange: 18...25, scoreRange: 54...60),
    HeartRateStatus(gender: .female, status: .athlete, ageRange: 26...35, scoreRange: 54...59),
    HeartRateStatus(gender: .female, status: .poor, ageRange: 65...Int.max, scoreRange: 84...Int.max)
]

现在您的getRhrStatus功能简洁明了:

func getRhrStatus(score: Int, age: Int, gender: HeartRateStatus.Gender) -> HeartRateStatus.Status {
    let result = rhrLookup.first {
        $0.scoreRange.contains(score) && $0.ageRange.contains(age) && $0.gender == gender
    }

    return result?.status ?? .unknown
}

测试:

let status = getRhrStatus(score: 50, age: 20, gender: .male)
print(status)

显然,这并不会检查重叠范围,但如果无法找到分数,则会返回unknown状态。