-2

On this page http://www.raywenderlich.com/85578/first-core-data-app-using-swift, many of the examples use let instead of var.

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

  let cell =
      tableView.dequeueReusableCellWithIdentifier("Cell")
      as UITableViewCell

  let person = people[indexPath.row]
  cell.textLabel!.text = person.valueForKey("name") as String?

  return cell
}

I see this in lots of tutorials. Is there some reason to use let in the above snippet over var?

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
4thSpace
  • 43,672
  • 97
  • 296
  • 475

1 Answers1

6

As a matter of fact, as a Swift programmer, the question to ask is why not use var over let? This is one area where Swift differs greatly in semantics from other languages such as C#.

The reason is because once you obtain a reference to a table cell, that reference is not going to change for the lifetime of each invocation of this method. There are no reassignments to cell, because there is simply no reason to do so. And therefore there is no reason to make cell a variable.

In general, in Swift, you should default to using let, and only use var if you need to reassign. Remember that let only prevents the constant itself from being reassigned; you can still mutate the object that is being referenced, as is being done in cell.textLabel!.text = person... in this case.

There are other, subtle differences that are further covered in Apple's documentation, but this applies to the majority of cases.

Community
  • 1
  • 1
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • Are there any compiler improvements to using `let` over `var`? Is `var` slower or anything? Is it just indicating semantics? – Bjorn Feb 17 '15 at 04:18
  • @Bjorn Tipling: The compiler will not let you reassign to a constant declared using `let`, and will error out if it notices you trying to do so, either by mistake or on purpose. – BoltClock Feb 17 '15 at 04:35
  • I'm comparing to c# where the above would both be variables. It seems strange to declare everything as constants. But I agree if the storage is never going to change, `let` works. – 4thSpace Feb 17 '15 at 04:44
  • @BoltClock: what is the bang syntax after textLabel? – 4thSpace Feb 17 '15 at 04:47
  • @4thSpace: That's known as forced unwrapping, where `cell.textLabel` is an optional and you're unwrapping its value when you know it always has a value at that specific point in time (so you don't have to do a nil check every time). In this case, it means you know that the cell *always* has a text label so you can just get the reference to the label immediately. – BoltClock Feb 17 '15 at 04:50
  • @4thSpace I believe you are talking about `!` symbol in the code. It is called optional forced unwrapping. You can read about them [here](http://stackoverflow.com/questions/24034483/what-is-an-unwrapped-value-in-swift) – Krishnabhadra Feb 17 '15 at 04:50
  • Ah - yes. I know it now. For those variables that can be null or nil. Thanks. – 4thSpace Feb 17 '15 at 04:55
  • Except for the compiler check are there any advantages for using `let` over `var`? Speed? Memory use? Like I'm not going to get mad at myself for using var over let in code only I will ever see. – Bjorn Feb 17 '15 at 05:04