Re: componentsSeparatedByString:
Re: componentsSeparatedByString:
- Subject: Re: componentsSeparatedByString:
- From: Ambroise Confetti <email@hidden>
- Date: Wed, 29 Oct 2003 21:27:10 +0100
Le 29 oct. 03, ` 20:06, Jonathan E. Jackel a icrit :
>
> I'm putting some items of the array returned by -[NSString
>
> componentsSeparatedByString:] into another array to read them back
>
> later. However, after having used these NSStrings a couple of times,
>
> they become empty (they are still valid NSString objects, but empty
>
> strings).
>
>
Some code would help.
My method looks like that:
NSMutableArray *tmpArray;
NSArray *myFunctionResult;
myFunctionResult = [NSMutableArray array];
while( (myString = some method result) != nil )
{
#ifdef VERSION_THAT_DOESNT_WORK
tmpArray = [[myString componentsSeparatedByString:aString]
mutableCopy];
[tmpArray removeObject:anotherString];
[myFunctionResult addObjectsFromArray:tmpArray];
[tmpArray release];
#else
tmpArray = [[NSMutableArray alloc]
initWithArray:[myString componentsSeparatedByString:aString]
copyItems:YES];
[tmpArray removeObject:anotherString];
[myFunctionResult addObjectsFromArray:tmpArray];
[tmpArray release];
#endif
}
return myFunctionResult;
>
> If I inspect them, it appears that they are instances of
>
> NSBigMutableString (which I guess is part of the NSMutableString class
>
> cluster). I don't use mutable strings in that part of my application,
>
> so I can't be the one altering their content. I tried to -[copy] them
>
> before putting them in my array, and it solved the problem.
>
> My deduction: the strings returned by -[NSString
>
> componentsSeparatedByString:] are mutable strings (strange, but
>
> alright) which are somehow modified later (for some definition of
>
> later...), and *not by me*.
>
>
First, you can't count on getting a mutable string back. It might be
>
mutable because NSString decides that a mutable string is best in some
>
particular set of circumstances. Even if you know how NSString makes
>
that
>
decision today, the method could change in a later version of the
>
framework.
>
So don't assume.
Oh, I assume nothing. In fact, I don't care as long as I get some
NSString subclass, and my code doesn't rely on anything else. I just
observe that I get something mutable, so that's not impossible for it
to change over time (though that's just crazy).
>
Are you really sure you are accessing the same strings every time?
>
Are you
>
really sure they don't start out empty? Try to figure out exactly
>
when the
>
value changes (using the debugger of course).
I spent a couple of hours yesterday doing that. In fact, my class is an
NSTextFormatter subclass which transforms a string into an array (for
Google-like search, where each word is transformed into an item except
for words enclosed by double quotes which are seen as a single "word")
and vice versa. The built array is then used by some object which gets
it with [theFormattedTextField objectValue] and performs the search. I
checked about a hundred times that items retrieved from the array are
only used in situations like [theString someNSStringMethod], so they
shouldn't be altered even if they are mutable.
Then, to update my user interface, I get the array back from the
searching object (which retains it) and do [theFormattedTextField
setObjectValue:theRetrievedArray].
I have put breakpoints in all methods that use the array at some time
(the formatter's methods and methods of the object performing the
search).
My objectValueForString: formatting method always returns an array with
a non-empty string. In the first following calls to
stringForObjectValue:, everything's alright. But at some time, the
string in the array becomes empty.
>
> Has anyone ever experienced that? It looks like it only happens if
>
> -[NSString componentsSeparatedByString:] returns a singleton.
>
>
What do you mean by "returns a singleton"? It always returns a regular
>
instance of NSArray (or a subclass).
I mean an array with 1 object. It doesn't (seem to) happen when the
returned array contains 2 or more objects (i.e. when I search 2 or more
terms).
Ambroise
http://www.cellulo.info/
ICQ 4508259
AIM atvaark
[demime 0.98b removed an attachment of type text/directory which had a name of Ambroise Confetti.vcf]
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.