After upgrading to Xcode 8 and converting all my code to Swift 3, I have troubles compiling swift resources. It takes a very long time, and my computer gets super laggy and
Believe it or not, this is the piece of code that was causing the problem for me. With it in, the compile takes about 30 minutes. If I simply comment out that chunk of code, it compiles in less than 30 seconds.
let params : [String: Any] = [
"person_id" : kPersonId,
"person_promo_id" : promo.personPromoId!,
"promo_page_id" : promo.promoPageId!,
"seq_no" : promo.seqNo!,
"promo_type" : promo.promoType!,
"page_name" : promo.pageName!,
"image_name" : promo.imageName!,
"start_date" : promo.startDate!,
"end_date" : promo.endDate!,
"website" : promo.website!,
"facility_name" : promo.facilityName!,
"address" : promo.street!,
"city" : promo.city!,
"prov_state_cd" : promo.provState!,
"country_cd" : promo.country!,
"contact_name" : promo.contactName!,
"contact_phone" : promo.contactPhone!,
"latitude" : promo.latitude!,
"longitude" : promo.longitude!,
"bgColorRed" : promo.bgColorRed!,
"bgColorGreen" : promo.bgColorGreen!,
"bgColorBlue" : promo.bgColorBlue!,
"promoCategories" : promoCat
]
Based on this and other things I have read, I would hunt for a case where you're assigning values to a large or nested dictionary with an Any
or AnyObject
in the definition. I'm guessing that it's the Any
that is sending the compiler off on a wild good chase.
If you check your log where it fails, it should have the error right at the object that failed. This should give you a clue as to what file to look in.
Edit: @Jay Chow, this is how I solved the compiler problem with the code above:
var params : [String : Any] = [:]
params["person_id"] = kPersonId
params["person_promo_id"] = promo.personPromoId
params["promo_page_id"] = promo.promoPageId
params["seq_no"] = promo.seqNo
params["promo_type"] = promo.promoType
params["page_name"] = promo.pageName
params["image_name"] = promo.imageName
params["start_date"] = promo.startDate
params["end_date"] = promo.endDate
params["website"] = promo.website
params["facility_name"] = promo.facilityName
params["address"] = promo.street
params["city"] = promo.city
params["prov_state_cd"] = promo.provState
params["country_cd"] = promo.country
params["contact_name"] = promo.contactName
params["contact_phone"] = promo.contactPhone
params["latitude"] = promo.latitude
params["longitude"] = promo.longitude
params["bgColorRed"] = promo.bgColorRed
params["bgColorGreen"] = promo.bgColorGreen
params["bgColorBlue"] = promo.bgColorBlue
params["promoCategories"] = promoCat
My case had to do with appending too many programmatically created constraints to a view controller's view. I had multiple arrays of constraints defined at the class level as follows:
lazy var labelConstraints: [NSLayoutConstraint] = [...]
I was using this notation for all UI elements in my view. By the time I was done building the view, I had roughly 10 arrays of 3-5 NSLayoutConstraints.
I was then appending a concatenated array to the view's constraint array like so:
self.view.addConstraints(labelConstraints + buttonConstraints + viewConstraints, ...)
This line turned out to be the problem. It must be something with lazy initialization and inline array concat. Whatever the cause, I have fixed this using flatMap as follows.
let constraints = [labelConstraints, buttonConstraints, viewConstraints].flatMap{ $0 }
self.view.addConstraints(constraints)
In my case, Xcode 9, Swift 4, the compilator couldn't stand addition of 8 numbers:
let aBitPattern: UInt64 = ((UInt64(a) & UInt64(0xff)) << 0) +
((UInt64(b) & UInt64(0xff)) << 8) +
((UInt64(c) & UInt64(0xff)) << 16) +
((UInt64(d) & UInt64(0xff)) << 24) +
((UInt64(e) & UInt64(0xff)) << 32) +
((UInt64(f) & UInt64(0xff)) << 40) +
((UInt64(g) & UInt64(0xff)) << 48) +
((UInt64(h) & UInt64(0xff)) << 56)
Transforming to this solved the infinite compilation:
var aBitPattern: UInt64 = ((UInt64(a) & UInt64(0xff)) << 0);
aBitPattern += ((UInt64(b) & UInt64(0xff)) << 8);
aBitPattern += ((UInt64(c) & UInt64(0xff)) << 16);
aBitPattern += ((UInt64(d) & UInt64(0xff)) << 24);
aBitPattern += ((UInt64(e) & UInt64(0xff)) << 32);
aBitPattern += ((UInt64(f) & UInt64(0xff)) << 40);
aBitPattern += ((UInt64(g) & UInt64(0xff)) << 48);
aBitPattern += ((UInt64(h) & UInt64(0xff)) << 56);
Yeah, and sometimes the semicolons help the compilator to understand your code as well.
I always look forward to the new Swift compilator and it always is a huge disappointment from Apple.
In my case, when I found that issue the chunk of code was:
return realm2.objects(Alert.self).sorted(by: { (alert1, alert2) -> Bool in
return alert1.hour < alert2.hour &&
alert1.minute < alert2.minute &&
alert1.label < alert2.label
})
In my case, I was writing a class that implemented Equatable
and also had generic members. My issue was rooted in comparing the generic members with ==
, when the generic class didn't have a ==
operator implemented.
For example (using Variable
from RxSwift
):
class Foo: Equatable {
var fieldOne: Variable<String> = Variable("fieldOne")
var fieldTwo: Variable<String?> = Variable(nil)
public static func ==(lhs: Foo, rhs: Foo) -> Bool {
//should be marked as a compiler error, since Variable doesn't have ==
return lhs.fieldOne == rhs.fieldOne && lhs.fieldTwo == rhs.fieldTwo
}
}
I should have seen a compiler error for this since no definition of ==
exists for Variable
, but the compiler would crash (as well as the static analysis) before being able to show it. I was seeing memory usage of 60GB+ before the crash. Yikes.
It seems swift still has some lingering bugs when it comes to generics.