Re: Error unwrapping an optional which isn't an optional
Re: Error unwrapping an optional which isn't an optional
- Subject: Re: Error unwrapping an optional which isn't an optional
- From: Greg Parker <email@hidden>
- Date: Mon, 21 Sep 2015 16:03:55 -0700
> On Sep 21, 2015, at 11:01 AM, Alex Hall <email@hidden> wrote:
>
> Hi list,
> I'm setting up a binding using an NSController, which I want to connect the currently selected row in a table to the values of some UI controls. Put another way, whenever the table's selected row changes, the binding should cause some controls to display new information pulled from the row.
>
> It's not working, but the error I'm seeing makes no sense. I've switched to using tableViewSelectionDidChange in a delegate, and that works perfectly. I'm not desperate to solve this problem since my delegate does what I want; I'm more curious about why it might be happening.
>
> The key for the binding is my view controller .currentTweet. That property is computed, and I've marked it as dynamic and implemented keyPathsForValues. Yet, when I run it with the binding in place: "fatal error: unexpectedly got nil while unwrapping an optional value." The weird part: nowhere on the line in question do I use an optional. Here are two declarations. The first is where the second gets its value; the second is what my controller is bound to. Both of these happen in my ViewController subclass, outside of any methods.
>
> dynamic var currentTweet:Tweet? {
> get {
> return currentTab.tweetsList[tweetsTable.selectedRow] //Xcode points to this line as the problem
> }
> }
>
> dynamic var currentTweetID:String? {
> get {
> if let tempTweet=currentTweet, tempID = tempTweet.idAsString {
> return tempID
> }
> return nil
> }
> }
>
> Yes, both properties are optional, but the line Xcode is complaining about has no optionals at all.
How are currentTab and tweetsList and tweetsTable declared? Those are the obvious candidates for an optional access.
What is the stack trace of the error? Perhaps one of those references is itself a complex accessor that accesses an optional internally.
> I thought about putting the tweetsTable.selectedRow bit in an 'if let', but that won't work because it's not optional so Swift won't let it in an 'if let'.
selectedRow's "row index or -1" behavior is an obvious candidate for a Swift optional, but we don't currently have a good way to bridge such API with Objective-C.
--
Greg Parker email@hidden Runtime Wrangler
_______________________________________________
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