user1007522 user1007522 - 1 month ago 13
Swift Question

Xcode keeps building on Swift 3

I've converted my Swift 2.3 project to swift 3. Now the compiler doesn't throw any errors anymore but it keeps compiling. CPU is like 100% and it keeps compiling for like 50 minutes or more if you don't stop it.

Xcode keeps saying Building .. | Compiling Swift source files

In the build log it stops always on the same swift files. The swift files are just simple model classes so I don't know what the problem could be.

I had the same problem in swift 2 but that was caused by the

??
operator. I refactored the code to remove the
??
operator so it can't be this anymore.

How can I find out what slows down the compile time to endless?

My models all look the same:

class Test: InputContract {
var appointmentDate: Date!
var startTime: String!
var endTime: String!
var registerDescription: String!
var subjectKey: String!
var channelCode: String!
var relationManagerHrId: String = ""
var employeeUserCode: String = ""
var smsReminderMobileNumber: String = ""
var smsReminderMobileNumberSequence: String!
var contactPhoneNumber: String = ""
var contactPhoneNumberSequence: String!
var smsReminder: Bool = false

override func retrieveInputDictionary() -> NSDictionary {
return ["description" : self.registerDescription, "appointmentDate" : Utils.formattedDate(self.appointmentDate),
"startTime" : self.startTime, "endTime" : self.endTime, "subjectKey" : self.subjectKey, "channelCode" : self.channelCode, "smsReminder" : self.smsReminder ? "true" : "false", "relationManagerHrId" : self.relationManagerHrId, "employeeUserCode" : self.employeeUserCode,
"smsReminderMobileNumber" : self.smsReminderMobileNumber, "contactPhoneNumber" : self.contactPhoneNumber, "smsReminderMobileNumberSequence" : self.smsReminderMobileNumberSequence, "contactPhoneNumberSequence" : self.contactPhoneNumberSequence
]
}
}


InputContract is:

protocol InputDictionaryMapper {
func retrieveInputDictionary() -> NSDictionary
func retrievePublicInputDictionary() -> NSDictionary
}

class InputContract: Model, InputDictionaryMapper {

func retrieveInputDictionary() -> NSDictionary {
fatalError("Each inputContract implementation must implement it's own method: \(NSStringFromClass(type(of: self)))")
}

func retrievePublicInputDictionary() -> NSDictionary {
fatalError("Each inputContract implementation must implement it's own method: \(NSStringFromClass(type(of: self)))")
}

required init(json: JSON) {
fatalError("init(json:) has not been implemented")
}

override init() {
super.init()
}
}


And model is just a base class that has another init for json too.

When I run the analyser on the build log then all my models are taking soo long to create the NSDictionary. But Why?

Answer

So the problem was that we had a lot of dictionaries created like this:

let dict = ["key": value, "key2": value2]

If you rewrite this as

var dict: [String: Any] = [String: Any]()
dict["key"] = value
dict["key2"] = value2

then the compiler magically only takes 15 to 20 ms per model instead of 2000 ms per model.

You can try it yourself with the buildtime analyser app :-)