[ios] XCode 8非常慢的快速編譯


Answers

自從升級到Swift 3 / XCode 8後,我遇到了同樣的問題,它似乎是由大數組文字引起的,類似於this 。

我能夠通過向分配給數組文字的變量添加類型註釋來解決問題,例如

let array: Array<String> = ["1", "2", "3", "4", "5", "6", "7", "8"]

代替

let array = ["1", "2", "3", "4", "5", "6", "7", "8"]
Question

自從Swift 3和XCode8以來,我的項目編譯速度非常慢。 每次我將一個空行添加到文件中時,重新編譯需要一整分鐘。 當我檢查輸出時,沒有特定的文件需要很長時間。 (我也用這個工具測量它: https://github.com/RobertGummesson/BuildTimeAnalyzer-for-Xcodehttps://github.com/RobertGummesson/BuildTimeAnalyzer-for-Xcode

它似乎總是一次編譯4個文件。 “節奏”非常穩定。 只是很慢......

另外:每當我打開或切換文件時,可能需要很長時間才能獲得自動完成或錯誤/警告。

我可以檢查什麼? 我幾乎覺得我設置了一些標誌,只是像瘋了一樣拖慢構建速度。

編輯:這不是解決潛在問題的方法,但我花了一些時間將更多代碼轉移到框架上。 這有所不同(僅僅因為它每次都必須重新編譯較少的文件)。 這不應該是必要的,但它無法忍受......我當然仍然非常期待一個正確的解決方案。




通過避免使用Nil-Coalescing運算符和字符串連接,我能夠大大減少我快速的項目編譯時間。

在其他地方我有類似的東西:

let x = "one" + object.nullableProperty ?? ""

我改成了

let x = String(format: "one %@", object.nullableProperty ?? "")

我的編譯時間大幅下降 - 從20分鐘到20秒。




減慢編譯時間的一種常見做法是使用Array.appendString.append (或它們的+運算符等價物)。 對於String ,最好使用格式化的字符串,而不是

let hello = "Hello, "
let world = "World!"
let combinedString = hello + world

你應該使用

let combinedString = "\(hello)\(world)"

我不記得確切的加速,但對於那些特定的線,它大約是10次。 可以想像,除了最重要的項目之外,這幾乎不會有明顯的加速。 例如,我們的項目有數百個 Swift文件,以及許多Objective-C文件,我們的編譯時間通常是10分鐘或更長時間, 有時甚至是唯一的更改是非Swift文件




在我的情況下,我使用輔助函數在Firebase中保存一些數據。 該函數返回一個包含大約20個元素的字典,編譯大約需要40分鐘。 我的解決方案是初始化一個空字典,然後將這些項逐個添加到someDict 。 現在它在不到30秒的時間內編譯。 我希望它有所幫助。

之前

func toAnyObject() -> AnyObject {
  return
      ["BookingAmount":BookingAmount,
     "BookingNumber":BookingNumber,
     "PostCode":PostCode,
     "SelectedBathRow":SelectedBathRow,
     "SelectedBedRow":SelectedBedRow,
     "DateAndTime":DateAndTime,
     "TimeStampDateAndTime":TimeStampDateAndTime,
     "TimeStampBookingSavedInDB": TimeStampBookingSavedInDB,
     "FrequencyName":FrequencyName,
     "FrequecyAmount":FrequecyAmount,
     "insideCabinets": insideCabinets,
     "insideFridge": insideFridge,
     "insideOven": insideOven,
     "laundryWash": laundryWash,
     "interiorWindows": interiorWindows,
     "FullName":FullName,
     "SuppliesName":SuppliesName,
     "SuppliesAmount":SuppliesAmount,
     "FlatNumber":FlatNumber,
     "StreetAddress":StreetAddress,
     "PhoneNumber":PhoneNumber,
     "EmailAddress":EmailAddress] as AnyObject

}

  func toAnyObject() -> AnyObject {

    var someDict = [String : AnyObject]()
    someDict["BookingAmount"] = self.BookingAmount as AnyObject?
    someDict["BookingNumber"] = self.BookingNumber as AnyObject?
    someDict["PostCode"] = self.PostCode as AnyObject?
    someDict["SelectedBathRow"] = self.SelectedBathRow as AnyObject?
    someDict["SelectedBedRow"] = self.SelectedBedRow as AnyObject?
    someDict["DateAndTime"] = self.DateAndTime as AnyObject?
    someDict["TimeStampDateAndTime"] = self.TimeStampDateAndTime as AnyObject?
    someDict["TimeStampBookingSavedInDB"] = self.TimeStampBookingSavedInDB as AnyObject?
    someDict["FrequencyName"] = self.FrequencyName as AnyObject?
    someDict["FrequecyAmount"] = self.FrequecyAmount as AnyObject?
    someDict["insideCabinets"] = self.insideCabinets as AnyObject?
    someDict["insideFridge"] = self.insideFridge as AnyObject?
    someDict["insideOven"] = self.insideOven  as AnyObject?
    someDict["laundryWash"] = self.laundryWash as AnyObject?
    someDict["interiorWindows"] = self.interiorWindows as AnyObject?
    someDict["FullName"] = self.FullName as AnyObject?
    someDict["SuppliesName"] = self.SuppliesName as AnyObject?
    someDict["SuppliesAmount"] = self.SuppliesAmount as AnyObject?
    someDict["FlatNumber"] = self.FlatNumber as AnyObject?
    someDict["StreetAddress"] = self.StreetAddress as AnyObject?
    someDict["PhoneNumber"] = self.PhoneNumber as AnyObject?
    someDict["EmailAddress"] = self.EmailAddress as AnyObject?

    return someDict as AnyObject
}



Links