Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: unwrapping cause swift compile slowly

In func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell!{

this line of code makes swift compile extremely slow:

cell!.detailTextLabel.text = child.year! + " " + child.make! + " " + child.model!

It takes me 1 min and 44 secs to build the project if I have this line of code. And 99% of the time it stuck at "Compiling Swift source files". If I change this line to

cell!.detailTextLabel.text = " "//child.year! + " " + child.make! + " " + child.model!

It only take me 5 or 6 sec to build the project. I would like to know why this line of code will cause so much time compiling.

In my Child model, they are declared as :

var name:String?
var year:String?
var make:String?
var model:String?

and the init:

init(name:String!, ... ,year:String!, make:String!, model:String!, ...){
        self.name = name
       ...
        self.year = year
        self.make = make
        self.model = model
}

The part I construct a child:

Child(name:cName,...,year:cYear,make:cMake, model:cModel,...)
like image 426
fuiiii Avatar asked Jul 19 '14 17:07

fuiiii


1 Answers

Yes, I filed a bug report (17585851) on this slow compilation issue, and you should do the same; the more clear use cases Apple is sent, the better. My slow code was several occurrences of this form:

let title = obj.valueForProperty(MPMediaItemPropertyTitle) as? String
self.titles += title ? title! : ""

(which, as you can see, is doing nil testing / unwrapping). It was cumbersome but not difficult for me to work around the problem by doing the same thing in a different way, and you should do likewise. But file that bug report first!

like image 183
matt Avatar answered Sep 30 '22 13:09

matt