Error unwrapping an optional which isn't an optional
Error unwrapping an optional which isn't an optional
- Subject: Error unwrapping an optional which isn't an optional
- From: Alex Hall <email@hidden>
- Date: Mon, 21 Sep 2015 14:01:54 -0400
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. My only thought is that this binding is happening before the table is created, thus tweetsTable.selectedRow has no value? 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'.
As I said, I'm just curious about this, not desperate to fix it and totally lost like a couple weeks back. :) My delegate does a fine job doing what I want, and I might never use a binding for this job at all. The error is just such an odd one, considering there are no optionals on that line.
--
Have a great day,
Alex Hall
email@hidden
_______________________________________________
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