6

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?

9
  • 1. Have you tried to clean project/restarting Xcode/etc.? 2. Have you tried to compile this Swift file in a separate project? If not, please post the contents of the Swift file on which it hangs. Commented Sep 28, 2016 at 8:17
  • hello, i have same problem, you got any solution? Commented Sep 28, 2016 at 8:51
  • @fiks I edited my question with the class. Commented Sep 28, 2016 at 9:04
  • It might be a problem with type inference. Could you please try storing your dictionary in a variable, e.g. let dict: [String: Any] = //...initialisation. And then return dict as NSDictionary. Also, do you really need NSDictionary? Why don't you use normal Swift Dictionary? Commented Sep 28, 2016 at 9:12
  • The first on I already tried the variable is not the solution. I also think there is a problem. But I thought NSDictionary is interchangeable with Dictionary? So you suggest I remove NSDictionary and return Dictionary<String, Any> like this? Commented Sep 28, 2016 at 9:14

2 Answers 2

5

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 :-)

Sign up to request clarification or add additional context in comments.

4 Comments

actually, the first dictionary is actually an array :) Wouldn't it be enough to define let dict: [String: Any] = .... Type inference can be slow and declaring the type should be enough.
Sorry you are right the first one was an array. I edited the code. Yes ofcourse you can drop off the [String: Any]() part. It was just to make it clear.
What happens if you try let dict: [String: Any] = ["key": value, "key2": value2], ie the same thing but with a type defined?
The same happens. You really need to rewrite it the other way. That solved my endless compile but the project has almost 400 models...
0

I was facing the same problem after migrating the project to swift 3, but after lots of R&D I found a solution. It was taking time because of dictionaries and array created without data type.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.