11

I'm working my way through the Swift table demos, and all of them seem have this same error message under 6.0.1. Not sure how to tackle this:

enter image description here

jlehr
  • 15,557
  • 5
  • 43
  • 45
Edward Potter
  • 3,760
  • 4
  • 28
  • 39

3 Answers3

19

try this:

cell.textLabel!.text = self.tableData[indexPath.row]

And read this article about optionals here: Optionals in Swift

Update:

A better approach is now to use:

cell.textLabel?.text = self.tableData[indexPath.row]
derdida
  • 14,784
  • 16
  • 90
  • 139
  • Thanks, this also worked: cell.textLabel.text? = self.tableData[indexPath.row] – Edward Potter Sep 22 '14 at 00:09
  • This optionals, unwrapping, ?, ! is something I have never seen before. Would love to see it represented as an infographic of some kind. thanks! READING! :-) – Edward Potter Sep 22 '14 at 00:10
  • Check out my link. Here you find all information you need :) – derdida Sep 22 '14 at 00:11
  • 3
    This strikes me as a bad syntactical decision. Is this really necessary? It looks so clunky. I thought that, with Swift, we were trying to get away from stuff like this; it pollutes the nice.clean.dot.notation. It's also easy to mis-read the ! as a "NOT" operator, and the ? as ternary. – kmiklas Oct 10 '14 at 16:29
  • The better way to do it is with optional chaining: `cell.textLabel?.text = self.tableData[indexPath.row]`. Force unwrapping has its place and is convenient, but more often than not you are loading up your codebase with crash sites that will one day be the bane of your existence (or one of your teammates) when you start refactoring things. "Do this or crash" is a very troglodytic way to interact with your compiler when other more civilized and diplomatic options are available. :) – Patrick Lynch Jul 29 '15 at 07:36
  • It depends on your data for your table. If you´re checking it, when you populate your local array/dictionary - an optinal chaining here is not necessary. And i dont have any idea why you downvote that answer... – derdida Jul 29 '15 at 10:04
  • 1
    I'm talking about the way your code force unwraps the `textLabel` property instead of using any of other safe ways to do so, such optional binding (`if let`) or optional chaining (`?.`). Developers who are learning Swift are going to be plagued with crashes if everyone teaches them to force unwrap their optional values and ignore the rich suite of language features designed to do it safely, which is the point of having optionals in the first place. That's why I down voted your answer. – Patrick Lynch Jul 29 '15 at 17:34
3

In Xcode 6.1.1 you have to unwrap both the cell and textLabel:

cell!.textLabel!.text = "your text"

HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
BA_
  • 55
  • 7
0

If you look up the textLabel property documentation here, you will find it defined as:

var textLabel: UILabel? { get }

This indicates that the property is an Optional. That means that it might contain a valid value or it might be a nil. If you are 100% sure that your cell will have this textLabel populated, then you can use the method suggested by derdida i.e:

cell.textLabel!.text = self.tableData[indexPath.row]

However, as Patrick Lynch rightly pointed out, you are just setting yourself up for future run-time crashes (time bombs) if you do this to all Optionals you encounter. The best practice is to write code that will gracefully handle the case whereby an Optional is nil. In that case, the whole expression evaluates to nil. Something like this:

cell.textLabel?.text = self.tableData[indexPath.row]

Although I have no infographic for this topic, a very good write-up exists here and will get you up to speed in no time. I hope that helps.

Frak
  • 832
  • 1
  • 11
  • 32