Re: Stupid ! and ?
Re: Stupid ! and ?
- Subject: Re: Stupid ! and ?
- From: Marco S Hyman <email@hidden>
- Date: Sat, 25 Apr 2015 10:44:46 -0700
On Apr 25, 2015, at 7:59 AM, William Squires <email@hidden> 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.
If you know and can prove that textLabel is NEVER nil then use !.
! translates to “I know this is not nil and if I’m wrong it’s OK to throw an
exception that terminates the program”.
If textLabel could be nil then use ?
Also, you are returning cell which is an optional but your function says it
is returning a UITableViewCell. Either return cell! or change the function
to return an optional UITableViewCell.
I find optionals to be fantastic. It’s interesting to find how optional
values thread their way through an application until you get to the point
where they must be unwrapped… and then you find it is nil because you made
a coding error 7 levels up the call tree.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden