【问题标题】:Slow Swift Build when using nested helper function in function在函数中使用嵌套辅助函数时 Swift Build 缓慢
【发布时间】:2015-05-01 15:05:44
【问题描述】:

我正在使用一个数组来返回是否选择了我的任何嵌套按钮。

为了方便我,我做了这个内部函数。

一切都变得非常缓慢。

我去掉了辅助函数,构建时间几乎是瞬间完成的。

不确定为什么类型推断在这里不起作用? (如果这是问题)

我已阅读此Why is swift compile time so slow?,但我还不知道为什么这些特定的行会停滞不前。

func areAnyAMPMButtonsSelected() -> [Bool] {


     func nilToBool(optional: AppointmentDatePickerAMPMButton?) -> Bool                      {
          return (optional != nil) ? optional!.selected : false
      }  

    return [
    nilToBool(self.sundayAMButton),
    nilToBool(self.mondayAMButton),
    nilToBool(self.tuesdayAMButton),
    nilToBool(self.wednesdayAMButton),
    nilToBool(self.thursdayAMButton),
    nilToBool(self.fridayAMButton),
    nilToBool(self.saturdayAMButton),

    nilToBool(self.sundayPMButton),
    nilToBool(self.mondayPMButton),
    nilToBool(self.tuesdayPMButton),
    nilToBool(self.wednesdayPMButton),
    nilToBool(self.thursdayPMButton),
    nilToBool(self.fridayPMButton),
    nilToBool(self.saturdayPMButton)
]

编译器停在这里

1.  While type-checking 'getValidTimesArray' at /Users/gustavo/Projects/vm/yips-ios/YIPS-iOS/AppointmentDatePickerView.swift:104:5

2。在 [/Users/gustavo/Projects/vm/yips-ios/YIPS-iOS/AppointmentDatePickerView.swift:110:16 - line:126:9] RangeText="[ nilToBool(self.sundayAMButton), nilToBool(self.mondayAMButton), nilToBool(self.tuesdayAMButton), nilToBool(self.wednesdayAMButton), nilToBool(self.thursdayAMButton), nilToBool(self.fridayAMButton), nilToBool(self.saturdayAMButton),

        nilToBool(self.sundayPMButton),
        nilToBool(self.mondayPMButton),
        nilToBool(self.tuesdayPMButton),
        nilToBool(self.wednesdayPMButton),
        nilToBool(self.thursdayPMButton),
        nilToBool(self.fridayPMButton),
        nilToBool(self.saturdayPMButton)
    ]"

【问题讨论】:

  • 可能duplicate?
  • @Antonio - 在这种情况下,它的类型检查卡住了,而不是索引那么不同的问题。类型检查需要在这里清除,我想知道是否可以在没有一些淫秽重复的情况下完成
  • 您是否尝试将数组减少到仅 3 或 4 个元素,仅用于测试?在我遇到编译缓慢的少数情况下,解决方案一直是将初始化分成多行。
  • 不清楚你在问什么。什么样的答案会让你在这里开心?
  • @matt - 了解类型检查系统。否定的答案,比如这是不可能的,都是超级好的。我需要知道它是如何工作的。

标签: swift


【解决方案1】:

我不知道它是否解决了问题,但值得检查 - 仅使用属性创建数组,然后使用 map 方法将它们中的每一个转换为 nilToBool 函数的返回值:

return [
    self.sundayAMButton,
    self.mondayAMButton,
    self.tuesdayAMButton,
    self.wednesdayAMButton,
    self.thursdayAMButton,
    self.fridayAMButton,
    self.saturdayAMButton,
    self.sundayPMButton,
    self.mondayPMButton,
    self.tuesdayPMButton,
    self.wednesdayPMButton,
    self.thursdayPMButton,
    self.fridayPMButton,
    self.saturdayPMButton
    ].map(nilToBool)

【讨论】:

  • 这个还是慢!但就代码而言,这是最优雅的解决方案:)
【解决方案2】:

这里还是很慢:

    1.  While type-checking 'getValidTimesArray' at /Users/gustavo/Projects/vm/yips-ios/YIPS-iOS/AppointmentDatePickerView.swift:104:5
    2.  While type-checking expression at [/Users/gustavo/Projects/vm/yips-ios/YIPS-iOS/AppointmentDatePickerView.swift:110:16 - line:126:9] RangeText="[
        nilToBool(self.sundayAMButton) as Bool,
        nilToBool(self.mondayAMButton) as Bool,
        nilToBool(self.tuesdayAMButton) as Bool,
        nilToBool(self.wednesdayAMButton) as Bool,
        nilToBool(self.thursdayAMButton) as Bool,
        nilToBool(self.fridayAMButton) as Bool,
        nilToBool(self.saturdayAMButton) as Bool,

        nilToBool(self.sundayPMButton) as Bool,
        nilToBool(self.mondayPMButton) as Bool,
        nilToBool(self.tuesdayPMButton) as Bool,
        nilToBool(self.wednesdayPMButton) as Bool,
        nilToBool(self.thursdayPMButton) as Bool,
        nilToBool(self.fridayPMButton) as Bool,
        nilToBool(self.saturdayPMButton) as Bool
    ]"

在这种情况下仍然很慢:

    var returnArray: [Bool] = []


    returnArray  = [
                nilToBool(self.sundayAMButton) as Bool,
                nilToBool(self.mondayAMButton) as Bool,
                nilToBool(self.tuesdayAMButton) as Bool,
                nilToBool(self.wednesdayAMButton) as Bool,
                nilToBool(self.thursdayAMButton) as Bool,
                nilToBool(self.fridayAMButton) as Bool,
                nilToBool(self.saturdayAMButton) as Bool,

                nilToBool(self.sundayPMButton) as Bool,
                nilToBool(self.mondayPMButton) as Bool,
                nilToBool(self.tuesdayPMButton) as Bool,
                nilToBool(self.wednesdayPMButton) as Bool,
                nilToBool(self.thursdayPMButton) as Bool,
                nilToBool(self.fridayPMButton) as Bool,
                nilToBool(self.saturdayPMButton) as Bool
    ]

    return returnArray

但它在这里变得非常快!虽然这在某种程度上是很多代码:

    var returnArray: [Bool] = []

    returnArray.append(nilToBool(self.sundayAMButton) as Bool)
    returnArray.append(nilToBool(self.sundayAMButton) as Bool)
    returnArray.append(nilToBool(self.mondayAMButton) as Bool)
    returnArray.append(nilToBool(self.tuesdayAMButton) as Bool)
    returnArray.append(nilToBool(self.wednesdayAMButton) as Bool)
    returnArray.append(nilToBool(self.thursdayAMButton) as Bool)
    returnArray.append(nilToBool(self.fridayAMButton) as Bool)
    returnArray.append(nilToBool(self.saturdayAMButton) as Bool)
    returnArray.append(nilToBool(self.sundayPMButton) as Bool)
    returnArray.append(nilToBool(self.mondayPMButton) as Bool)
    returnArray.append(nilToBool(self.tuesdayPMButton) as Bool)
    returnArray.append(nilToBool(self.wednesdayPMButton) as Bool)
    returnArray.append(nilToBool(self.thursdayPMButton) as Bool)
    returnArray.append(nilToBool(self.fridayPMButton) as Bool)
    returnArray.append(nilToBool(self.saturdayPMButton) as Bool)

    return returnArray

这是我选择的解决方案,非常快。

    return [
        (self.sundayAMButton    != nil)   ?  self.sundayAMButton.selected     : false,
        (self.mondayAMButton    != nil)   ?  self.mondayAMButton.selected     : false,
        (self.tuesdayAMButton   != nil)   ?  self.tuesdayAMButton.selected    : false,
        (self.wednesdayAMButton != nil)   ?  self.wednesdayAMButton.selected  : false,
        (self.thursdayAMButton  != nil)   ?  self.thursdayAMButton.selected   : false,
        (self.fridayAMButton    != nil)   ?  self.fridayAMButton.selected     : false,
        (self.saturdayAMButton  != nil)   ?  self.saturdayAMButton.selected   : false,

        (self.sundayPMButton    != nil)   ?  self.sundayAMButton.selected    : false,
        (self.mondayPMButton    != nil)   ?  self.mondayAMButton.selected    : false,
        (self.tuesdayPMButton   != nil)   ?  self.tuesdayAMButton.selected   : false,
        (self.wednesdayPMButton != nil)   ?  self.wednesdayAMButton.selected : false,
        (self.thursdayPMButton  != nil)   ?  self.thursdayAMButton.selected  : false,
        (self.fridayPMButton    != nil)   ?  self.fridayAMButton.selected    : false,
        (self.saturdayPMButton  != nil)   ?  self.saturdayAMButton.selected  : false
    ]

【讨论】:

    猜你喜欢
    • 2013-11-01
    • 1970-01-01
    • 2019-01-17
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 2016-04-25
    • 2018-05-07
    相关资源
    最近更新 更多