Re: Connecting NSTextStorage to a string
Re: Connecting NSTextStorage to a string
- Subject: Re: Connecting NSTextStorage to a string
- From: Kyle Sluder <email@hidden>
- Date: Wed, 28 Dec 2016 19:37:17 -0800
> On Fri, Dec 23, 2016, at 09:16 PM, Daryle Walker wrote:
> The text controls on macOS use a NSTextStorage object to retain their
> text. That object is a extension of an attributed string. Let's say you
> use a NSString (or similar) in your document model. How should I get
> changes on one string to get mirrored on the other?
It depends on what you’re trying to do. The Cocoa text system is set up for two modes: editing rich text, and editing non-rich text.
In the rich-text case, the NSTextStorage object is intended to *be* the model object. It’s a long-lived object with its own optimized mutation scheme (-beginEditing/-endEditing and the mutators it inherits from NSMutableAttributedString), which is married to one particular NSTextView instance in the window. This of course means it’s not a great fit for frameworks like Core Data or Cocoa Bindings which expect model object properties to be simple value types. You can bridge this gap with various techniques, such as registering an object as the NSTextStorage’s delegate that posts your own notifications or triggers change notifications for a synthesized Core Data property.
The non-rich-text mode is more straightforward. In this mode, the NSTextStorage is permanently married to the window’s field editor, and as focus moves around the window its contents are batch-replaced with the underlying control’s stringValue as focus moves around the window. The controller is expected to listen to NSControl text editing notifications (possibly by acting as the control’s delegate) to know when to push values to the model. It is expected to use some other means, such as KVO, to push values from the model to the control. Cocoa Bindings intends to do this two-way marshaling on your behalf.
--Kyle Sluder
>
> I think there are begin/end-editing flag methods on text controls, but
> how would I go the other way?
>
> Sent from my iPhone
_______________________________________________
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