Need Help

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Need Help

webdesign909


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 [hidden email].
To post to this group, send email to [hidden email].
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.
Reply | Threaded
Open this post in threaded view
|

Re: Need Help

Sam Stigler
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 <[hidden email]> 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 [hidden email].
To post to this group, send email to [hidden email].
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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/80E31E10-898C-4906-BAC4-B81C8594FFEF%40gmail.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Need Help

Brent Royal-Gordon
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


On Wed, Dec 31, 2014 at 10:39 AM, Sam Stigler <[hidden email]> wrote:

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 <[hidden email]> 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 [hidden email].
To post to this group, send email to [hidden email].
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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/80E31E10-898C-4906-BAC4-B81C8594FFEF%40gmail.com.
For more options, visit https://groups.google.com/d/optout.


--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/1420183589413.937e5fb8%40Nodemailer.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Need Help

GoDoStartupNow
In reply to this post by Sam Stigler

On Wed, Dec 31, 2014 at 1:38 PM, Sam Stigler <[hidden email]> wrote:
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 <[hidden email]> 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 [hidden email].
To post to this group, send email to [hidden email].
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.

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/80E31E10-898C-4906-BAC4-B81C8594FFEF%40gmail.com.

For more options, visit https://groups.google.com/d/optout.

--
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 [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/swift-language/CAOZ%2Bm26ejATYEX-apXyjGwrg05wCtHeYOWFFkROVt8vtS24UqQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.