Re: Can't access by data via KVC during KVO
Re: Can't access by data via KVC during KVO
- Subject: Re: Can't access by data via KVC during KVO
- From: Quincey Morris <email@hidden>
- Date: Mon, 27 Mar 2017 15:25:37 -0700
On Mar 27, 2017, at 14:48 , Daryle Walker <email@hidden> wrote:
>
> The message observation points to the object controller’s “selection”, then a specific property of my model (of type “String?”).
>> Could not cast value of type '_NSStateMarker' (0x7fffa3003cf8) to 'NSString' (0x7fffa397df38).
>> 2017-03-27 16:36:33.978709 XNW[39160:4830169] Could not cast value of type '_NSStateMarker' (0x7fffa3003cf8) to 'NSString' (0x7fffa397df38).
>
> What is this “NSStateMarker” class? And why does it prevent me from accessing the data?
In short, you shouldn’t be using the “selection” property as if it means something for an object controller. Yes, you *can* select an object in the object controller’s content as *the* selection, but it doesn’t help you with anything — it doesn’t represent anything in the UI.**
By definition, the “selection” is a proxy for an object (NSObjectController) or an array of objects (NSArrayController, which is a *subclass* of NSObjectController), but it can also represent a marker value, indicating no selection or multiple selection or (IIRC) some other possibilities. I assume your crash occurred because at the time you referenced “selection” it was set to one of these markers, and hence not castable to a String.
Programming (in the sense of writing actual code) against NS…Controller objects is a horrible and dangerous experience, because they’re black boxes and you’ll never know what they’re really doing internally. I strongly, strongly recommend that you program against the data model directly. There’s no value (that I know of) in referencing a NSObjectController as you have. NSArrayController is a bit less clear, because if you’re using it to fetch, sort and filter Core Data records, then you sort of think you need to use its arrangedObjects property. Still, you don’t have to go through a NSArrayController — there’s nothing wrong with fetching, sorting and filtering Core Data records yourself.
** I’ve never really understood for sure, but I think the point of having a selection is that (by default in IB) NSObjectController models a NSMutableDictionary, and it can distinguish between multiple keys “within” the “dictionary” that are in most cases just different properties of the content object. By binding to “selection” and switching the selection, you can switch which key supplies a value to a single UI field (like a primitive master-detail arrangement).
In fact, what normally happens is that the keys just represent independent objects accessible via the content object, and each of them is bound directly to its own UI element. In this case, the selection basically has no meaning.
_______________________________________________
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