Xcode 8.0 Swift 3.0 slow indexing and building

后端 未结 16 2149
天涯浪人
天涯浪人 2020-12-07 17:12

I\'ve installed Xcode 8.0 and converted Swift 2.2 to 3.0 (that process also took a lot of time, I just left my Mac running all night). I have not a big project (about 20 fil

16条回答
  •  醉话见心
    2020-12-07 17:37

    I had a function that took over a minute to compile, and after some investigation, I found that the culprit was checking if enough time had elapsed from a stored date:

    let myStoredDate: Double = // Double representing a time in the past
    
    // if at least one week (60 * 60 * 24 * 7 seconds) has passed since myStoredDate
    if Date().timeIntervalSince1970 - myStoredDate > (60 * 60 * 24 * 7){
        // do stuff
    }
    

    This code would take over 10 seconds to compile — coupled with this code being repeated with different numbers multiple times, it was causing compilation to take way too long. I was able to fix this by pre-computing the interval

    let myStoredDate = // Double representing a time in the past
    
    //it is important to explicitly specify that the variable is a Double
    let interval: Double = 60 * 60 * 24 * 7
    
    if Date().timeIntervalSince1970 - myStoredDate > interval{
        // do stuff
    }
    

    After doing this with the ~10 times I was checking, the compilation time was cut from over a minute to just a few milliseconds.

    It's extremely likely that this problem also occurs with the combination of type-inferance and math elsewhere, so ensure that nothing like this happens anywhere else in your code.

提交回复
热议问题