6

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,...)
fuiiii
  • 1,359
  • 3
  • 17
  • 33
  • You should not ask two questions in one. The slow compilation issue is a question. When unwrapping is necessary is a different question! If you ask the second question separately I'll try to answer it separately. – matt Jul 19 '14 at 17:47
  • @matt Thanks, see my second question here.http://stackoverflow.com/questions/24844495/swfit-strange-behavior-about-unwrapping – fuiiii Jul 19 '14 at 20:07

2 Answers2

4

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!

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • 1
    How did you do it in a different way and make the compile faster? And how can I fire a bug to Apple? Thanks! – fuiiii Jul 19 '14 at 20:01
  • I chronicled the cases that I ran into here: http://stackoverflow.com/questions/24943051/xcode-and-swift-why-is-swift-compiling-slowly-halt-and-catch-fire-ram-cpu-1 and reported a bug also referencing yours. – Chris Conover Jul 24 '14 at 20:35
0

You need to do unwrapping when your types are declared as optionals. In swift an optional type is a type that may or may not have a value. For example i could declare a string as:

var collegeName : String?

The "?" at the end of type declaration shows that the collegeName may or may not have a value and in order to get that value you will have to unwrap it in order to get its value by using ! operator.

As far as i can tell in your case the name field is declared as a string while year , make and model are declared as optional strings that is why you need to unwrap those fields to get the value out of them.

You can also declared a type using "!" operator like:

var collegeName : String!

This means that this is an optional string but it is automatically unwrapped for you so you don't need to use the "!" afterwards to unwrap this optional.

salman140
  • 1,510
  • 10
  • 11
  • I agree with what you say about optionals. The child name was declared as optional, too. That's why I'm confused since I don't need to unwrapping it. I just forgot to include it's code in my question. var name:String? – fuiiii Jul 19 '14 at 19:52
  • second question is in here now http://stackoverflow.com/questions/24844495/swfit-strange-behavior-about-unwrapping – fuiiii Jul 19 '14 at 20:09