ios - swift 中的 3d 范围问题数组

标签 ios swift xcode compilation

我有一个范围表,基本上,我想从中找到我的分数。我的 table 看起来像 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 文件。如果我评论上面的代码,它很容易编译。)请为此提出更好的方法。

提前致谢。

最佳答案

当您没有显式指定文字类型时,Swift 编译器在编译大型数组文字时会遇到问题。它使 Swift 的类型推断系统负担过重,并且需要很长时间或永远不会完成。

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

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

let ageForRhr: [CountableClosedRange<Int>] = [18...25,26...35,36...45,46...55,56...65,65...100]

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

let womenRhrScore: [[CountableClosedRange<Int>]] = [ [54...60 , 61...65,66...69,70...73,74...78,79...84,85...200] as [CountableClosedRange<Int>],
                      [54...59 , 60...64,65...68,69...72,73...76,77...82,83...200] as [CountableClosedRange<Int>],
                      [54...59 , 60...64,65...69,70...73,74...78,79...84,85...200] as [CountableClosedRange<Int>],
                      [54...60 , 61...65,66...69,70...73,74...77,78...83,84...200] as [CountableClosedRange<Int>],
                      [54...59 , 60...64,65...68,69...73,74...77,78...83,84...200] as [CountableClosedRange<Int>],
                      [54...59 , 60...64,65...68,69...72,73...77,78...84,85...200] as [CountableClosedRange<Int>]]

您的代码存在编译错误,现在 Swift 已解除卡住,您可以查看并修复这些错误。

关于ios - swift 中的 3d 范围问题数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44589651/

相关文章:

ios - 网络连接应仅在更改时出现

ios - 有没有办法在保持同一页面的同时从 UIPageViewController 中更改标签文本?

objective-c - 如何创建将处于 DST 模式的 NSDate

ios - 如何在 SwiftUI 上使用 CAEmitterLayer 制作动画?

ios - 核心数据 + swift Int = 崩溃

ios - 如何根据 PFUser 字段从我的查询中过滤掉 PFObjects

ios - 如何在 iOS 中使用现有的 SQLite 数据库?

ios - 在 Swift 中从 ATR 确定卡号

ios - 在 Xcode 9 之前创建的项目中重新组织组和文件夹

ios - 如何在Azure托管代理上使用特定的iOS版本?