Re: Copying NSTextStorage
Re: Copying NSTextStorage
- Subject: Re: Copying NSTextStorage
- From: Ross Carter <email@hidden>
- Date: Tue, 03 Jun 2008 14:19:34 -0400
On Jun 3, 2008, at 12:51 PM, Kyle Sluder wrote:
On Tue, Jun 3, 2008 at 11:15 AM, Gordon Apple <email@hidden> wrote:
I tried exactly that. It did nothing but a horrendous crash when I
tried to type text. I couldn't even trace it. I never even got to
the copy
part. I got the same result with a totally empty subclass.
Shouldn't it
have worked the same?s What gives with that?
It might have something to do with the fact that, according to the
documentation, NSTextStorage is a "semiconcrete subclass of
NSMutableAttributedString." What confuses me about this is that the
words "concrete" and "abstract" have very well-defined meanings...
"semiconcrete" is bizarre and meaningless. Doesn't that just mean
it's abstract?
--Kyle Sluder
Gordon, if I understand correctly, the situation is this: you have
created a class Shape that contains an NSDictionary object. You are
using an instance of NSTextStorage as the object for the key "text".
You need to copy the dictionary.
First I will echo what Jens has said: there's a strong probability
that NSAttributedString or NSMutableAttributedString will do what
need. NSTextStorage adds two capabilities to
NSMutableAttributedString: it communicates with one or more
NSLayoutManagers that are involved in laying out the attributed string
in one or more NSTextContainers, and it supports some scripting
commands. If the NSTextStorage object resident in your dictionary has
never had a layout manager added to it, and is not supporting
scripting, then it's likely going about just acting like a
NSMutableAttributedString, without doing any NSTextStorage tricks at
all.
As you say, Sketch uses a NSTextStorage object. It's in the SKTText
class, which supports scripting, so it makes sense to use an
NSTextStorage object. But "copy" or "mutableCopy" are never sent to
that object. The copy withZone: method says:
[[copy contents] setAttributedString:[self contents]];
[copy contents] causes copy to init a new NSTextStorage object, and
setAttributedString: is used to copy the text from the source. So you
see that Sketch "copies" an NSTextStorage object by creating a new
object and calling setAttributedString:. That's how you should copy
your NSTextStorage, too, IMHO.
The notion of copying an NSTextStorage object does not make much sense
to me. A happy working NSTextStorage object is busy updating
NSLayoutManager objects and sending notifications. If you were to copy
it, would you want the copy to inherit those NSLayoutManager
references, too? That would definitely muck things up, because a
layout manager is emphatically monogamous: it wants one and only text
storage. If you didn't want the copy to inherit the layout manager
references, then you might as well just copy the text storage's
attributedString. That's why I say that the notion of copying does not
really apply to NSTextStorage.
Kyle, AFAICT, NSTextStorage is the only Cocoa class deemed
"semiconcrete." I guess it means that you can instantiate
NSTextStorage objects as if they were concrete, but you can't subclass
them without special effort. Maybe a documentation bug would be in
order.
Ross
_______________________________________________
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