> On Apr 25, 2015, at 7:59 AM, William Squires <wsqui...@satx.rr.com> wrote:
> 
> Where I'm running into problems is this line of code:
> 
> func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: 
> NSIndexPath) -> UITableViewCell
> {
> var cell = tableView.dequeueReusableCellWithIdentifier(simpleTableIdentifier) 
> as? UITableViewCell
> if (cell == nil)
>  {
>  cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: 
> simpleTableIdentifier)
>  }
> cell!.textlabel.text = dwarves[indexPath.row] // Problem here
> return cell
> }
> 
> Xcode is complaining that a "?" or "!" is needed after "textLabel", and 
> before the ".text", but I don't really understand which one to use.

Using ‘?' here will give you the objC behavior of doing nothing if textLabel is 
nil.  Using ‘!’ would crash if it ended up as nil for some reason.  When in 
doubt use ‘?'

> Another line of code that confuses me is the first line:
> 
> var cell = tableView.dequeueReusableCellWithIdentifier(simpleTableIdentifier) 
> as? UITableViewCell
> 
> yet they use an "!" and not a "?" in the cell!.textLabel.text... line. 
> Shouldn't that be:
> 
> cell?.textLabel.text to match up with the ? in the var cell... line?

The ‘!’ basically means: Hey compiler, I know you think this could be nil, but 
trust me... I promise it won’t be.  In this case, the line above it checks to 
see if cell is nil and gives it a value, so we know it isn’t nil.  I still 
prefer to use ‘?’ or ‘if let’ in these cases though.

Thanks,
Jon
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to