How do I use Core Data with the Cocoa Text Input system?

Posted by the Joel on Stack Overflow See other posts from Stack Overflow or by the Joel
Published on 2010-06-12T15:35:53Z Indexed on 2010/06/12 15:42 UTC
Read the original article Hit count: 258

Hobbyist Cocoa programmer here. Have been looking around all the usual places, but this seems relatively under-explained:

  • I am writing something a little out of the ordinary. It is much simpler than, but similar to, a desktop publishing app. I want editable text boxes on a canvas, arbitrarily placed.

  • This is document-based and I’d really like to use Core Data.

Now, The cocoa text-handling system seems to deal with a four-class structure: NSTextStorage, NSLayoutManager, NSTextContainer and finally NSTextView. I have looked into these and know how to use them, sort of. Have been making some prototypes and it works for simple apps.

The problem arrives when I get into persistency. I don't know how to, by way of Cocoa Bindings or something else, store the contents of NSTextStorage (= the actual text) in my managed object context.

I have considered overriding methods pairs like -words, -setWords: in these objects. This would let me link the words to a String, which I know how to store in Core Data. However, I’d have to override any method that affects the text - and that seems a little much.

Thankful for any insights.

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about cocoa