Need Help

24 views
Skip to first unread message

webdesign909

unread,
Dec 31, 2014, 1:17:08 PM12/31/14
to swift-l...@googlegroups.com


Getting Error message and I don't know how to fix. Thank you in advance




Sam Stigler

unread,
Dec 31, 2014, 1:39:00 PM12/31/14
to webdesign909, swift-l...@googlegroups.com
Hi,

The problem here is that textLabel is a wrapped optional. You need to unwrap it before you can access its underlying UILabel. There are two ways to do this:

1) Force-unwrap it by writing something like: cell.textLabel!.text . This could cause a runtime crash, however, if the label is nil at runtime — for example, if you forgot to hook up an IBOutlet to it.

2) Use optional binding to only access the textLabel if it exists. This is the safer of the two options. You’d do that by, in this case, writing something like:

if let label = cell.textLabel {
label.text = restaurantNames[indexPath.row]
}


I hope this helps, and Happy New Year!

Sam

On Dec 31, 2014, at 12:17 PM, webdesign909 <webdes...@gmail.com> wrote:


Getting Error message and I don't know how to fix. Thank you in advance





--
You received this message because you are subscribed to the Google Groups "Swift Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swift-languag...@googlegroups.com.
To post to this group, send email to swift-l...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/5e3cf804-8514-4e2b-9379-72227889d6e6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Brent Royal-Gordon

unread,
Jan 2, 2015, 2:26:32 AM1/2/15
to Sam Stigler, webdesign909, swift-l...@googlegroups.com
It’s worth keeping in mind that which of these two approaches is “safer” is specific to the situation you’re in. If you expect that the optional may sometimes be nil, the “if let”-based approach is safer, because it doesn’t crash when there’s a nil value. But if the optional should never be nil in practice—for instance, if it’s an outlet that should always be connected by the time your code runs—you may consider it “safer” to crash rather than let the app display a user interface with missing or incorrectly-populated controls. This can be a fairly nuanced decision; it’s not as simple as “never force-unwrap anything”.


Brent Royal-Gordon
Sent from Mailbox


Tony SoftwareEngineer

unread,
Jan 2, 2015, 6:57:16 AM1/2/15
to Sam Stigler, webdesign909, swift-l...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages