Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 783
user1007522 Avatar asked Sep 28 '16 08:09

user1007522


People also ask

How do I switch from Swift 3 to Swift 5?

You need to redownload Xcode 10.1 from this page, convert your code to Swift 4, then redownload Xcode 10.2 and convert to Swift 5.

How long does an Xcode build take?

On a latest-generation machine, the same project may take around two minutes to compile. Also, Xcode uses a lot of RAM to operate.

Is Xcode better than Swift?

Xcode is only compatible with Apple technologies, while despite challenges, Swift provides the opportunity for cross-platform interoperability. Since Swift is a programming language, it does not provide any tools for development, but Xcode relies on tools and functionalities to build iOS and Mac applications.

Is SwiftUI same as Xcode?

SwiftUI is more than a framework. It is integrated into Xcode, enabling developers to build user interfaces much faster than ever before. You can edit the user interface of your application in code or in a visual editor that automatically reflects what your code translates to.


1 Answers

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

like image 97
user1007522 Avatar answered Sep 28 '22 01:09

user1007522