Skip navigation.
 
mlRe: Copying NSTextStorage
FROM : Ross Carter
DATE : Tue Jun 03 20:19:34 2008

On Jun 3, 2008, at 12:51 PM, Kyle Sluder wrote:

> On Tue, Jun 3, 2008 at 11:15 AM, Gordon Apple <<email_removed>> 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

Related mailsAuthorDate
mlCopying NSTextStorage Gordon Apple May 29, 20:03
mlRe: Copying NSTextStorage Jens Alfke May 30, 06:53
mlRe: Copying NSTextStorage Gordon Apple May 30, 17:13
mlRe: Copying NSTextStorage Kyle Sluder May 30, 22:16
mlRe: Copying NSTextStorage Gordon Apple Jun 1, 01:49
mlRe: Copying NSTextStorage Ross Carter Jun 1, 17:59
mlRe: Copying NSTextStorage Gordon Apple Jun 3, 17:15
mlRe: Copying NSTextStorage Kyle Sluder Jun 3, 18:51
mlRe: Copying NSTextStorage Ross Carter Jun 3, 20:19
mlRe: Copying NSTextStorage Kyle Sluder Jun 3, 22:21
mlRe: Copying NSTextStorage Alastair Houghton Jun 4, 00:09
mlRe: Copying NSTextStorage Sherm Pendley Jun 4, 10:14