真不是标题党,除了正常的算数运算,其他的千万要绕着"+"运算远一点啊。
这2天Xcode把我们团队坑瘸了,随着代码越来越多,这编译速度就越来越慢,越来越慢……今天我一个下午一边敲一遍编译,就编译成功了一次,简直是令人发指啊!😢
实在是不能忍了,满世界找了找原因,在stackoverflow上找到这个编译时间查看工具BuildTimeAnalyzer-for-Xcode,再回想起了一个零赞同的我以为是乱写的回答(当时觉得完全不可能才印象深刻),逐渐开始找到了头绪,项目编译慢的主要原因居然真是因为它说的"+"运算导致的。我靠!
下面是BuildTimeAnalyzer运行图。每一行都是一个swift文件(或某个swift文件的一个closure)的编译时间……项目里的这些文件大多有共同的特征:多个string
类型的变量以+
运算相连。恩,一个230行代码的文件,也就编译了4分钟而已嘛。
然后顺藤摸瓜找到了这个讨论http://stackoverflow.com/questions/29707622/bizarre-swift-compiler-error-expression-too-complex-on-a-string-concatenation,Honey的回答大致意思就是说swift3的 "+ " 运算严重超载了,运算时间会呈指数级的增长???!!!
怎么不用+
号,大家应该都知道吧,let combinedString = "\(hello) \(world)"
就好了。本文还没结束,我发现stackoverflow上其他一些开发者的说法,也一齐转过来了。
比如说Stefan Nestorov说不能这样使用??
,会编译慢:
let name = "\(someString ?? "")"
bibscy说Dictionary不能这样写
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
}
Ben Simon说Array不要这样写
let array = ["1", "2", "3", "4", "5", "6", "7", "8"]
而要写成这样:
let array: Array<String> = ["1", "2", "3", "4", "5", "6", "7", "8"]
let combinedArrays = array1 + array2
是不好的,要这样
[array1, array2].joined()
还让不让人用swift开发了!
所以用Xcode8.3开发swift应用的同学们,如果你发现你的编译速度慢的令人发指,一定要记得你看过本文啊!
Xcode8.3/Swift3简直是谋财害命。10天一新版,都发了3版了,还是这么烂啊!