Ever since Swift 3 and Xcode 8 my project compiles quite slowly. Every time I add so much as an empty line to a file, recompiling takes a full minute. When I check the outpu
In my case I was using a helper function to save some data in Firebase. That function was returning a dictionary with about 20 elements and it would take about 40 mins to compile.
My solution was to initialize an empty dictionary and then add the items one by one to someDict
. Now it compiles in less than 30 seconds. I hope it helps.
Before
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
}
After
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
}
I was able to greatly reduce my swift project compile times by avoiding the use the Nil-Coalescing Operator and string concatenation.
In otherwords where I had something like:
let x = "one" + object.nullableProperty ?? ""
I changed it to
let x = String(format: "one %@", object.nullableProperty ?? "")
My compile times have dropped drastically- from 20 minutes to 20 seconds.