RE: Mutable != Mutable ?
RE: Mutable != Mutable ?
- Subject: RE: Mutable != Mutable ?
- From: "Jonathan E. Jackel" <email@hidden>
- Date: Mon, 4 Aug 2003 15:30:40 -0400
Stefan's prescription should work, but his diagnosis seems wrong to me. You
don't get your error:
>
2003-08-04 11:18:54.643 Country Song[22148] Attempt to mutate
>
immutable NSString with replaceCharactersInRange:withString:
when you try to replace 3 characters in a mutable string with 20 or 200 or 2
million. In fact, you should get no error at all, no matter whether the
replacement string is bigger or smaller than what it is replacing.
NSMutableStrings change their capacity on the fly; in everyday use you'll
never get an error that a mutable string is "full." In any event, that's
not the error you got.
You get the error you got when . . . you attempt to mutate an immutable
string. Stefan's suggestion:
NSString *newLine = [NSMutableString stringWithFormat:@"%@%@%@",
FirstPartOf( theLine ),
[phraseArray objectAtIndex: randPhrase([phraseArray count])],
LastPartOf( theLine )
];
[theLine release];
theLine = newLine;
will work because he is creating a new string out of parts of another
string. (There's no need to use a mutable string for newLine; once newLine
is created it is not changed. ) His code won't generate your error because
it does not mutate any string, let alone an immutable one. On the downside,
he wants you to write two new functions to get the first and last parts of
theLine (although you could probaby use substringToIndex: and
substringFromIndex: instead).
My suggestion:
>
> theLine = [[[lineArray objectAtIndex:i] mutableCopy] autorelease];
>
>
>
> //then change theLine, then
>
>
>
> [lineArray replaceObjectAtIndex:i withObject:theLine];
also will work because it creates a mutable string copy of theLine, which
you can freely mutate. But you don't have to do any extra coding beyond
that.
Your statement:
>
I'm reading them in from a bundled XML property list. Into an
>
NSMutableArray.
suggests to me that you are using -initWithContentsOfFile: or
+arrayWithContentsOfFile: to create the array. This may give you a mutable
array, but the members of that array -- the strings -- are immutable.
Jonathan
>
-----Original Message-----
>
From: email@hidden
>
[mailto:email@hidden]On Behalf Of Michael Hanna
>
Sent: Monday, August 04, 2003 2:28 PM
>
To: email@hidden
>
Cc: email@hidden
>
Subject: Re: Mutable != Mutable ?
>
>
>
I'm reading them in from a bundled XML property list. Into an
>
NSMutableArray. I think the problem is the size of my tokens is
>
different from the size of my phrases. I believe the solution is to
>
divide-up theLine into three strings as suggested by Stefan.
>
>
Michael
>
>
On Monday, August 4, 2003, at 02:11 PM, Jonathan E. Jackel wrote:
>
>
> So how are you making the objects that are in lineArray?
>
>
>
>> -----Original Message-----
>
>> From: email@hidden
>
>> [mailto:email@hidden]On Behalf Of Michael Hanna
>
>> Sent: Monday, August 04, 2003 1:41 PM
>
>> To: email@hidden
>
>> Cc: email@hidden
>
>> Subject: Re: Mutable != Mutable ?
>
>>
>
>>
>
>> No, but thanks for the 'heads-up'. I imagine it's going to take me
>
>> about a year before I get fairly-well versed in writing Cocoa
>
>> programs.
>
>> It's a good thing that there's a relatively-high payoff during the
>
>> learning process; I can imagine many people would give up otherwise.
>
>>
>
>> Michael
>
>>
>
>> On Monday, August 4, 2003, at 12:07 PM, Jonathan E. Jackel wrote:
>
>>
>
>>>> theLine = [lineArray objectAtIndex:i]; // retrieve a line
>
>>>
>
>>> How are you making the objects that are in lineArray? Are you, by
>
>>> any
>
>>> chance, using NSString's componentsSeparatedByString method? Or
>
>>> using
>
>>> an
>
>>> NSScanner? If so, your array contains immutable strings. Simply
>
>>> assigning
>
>>> them to an NSMutableString * doesn't turn them into mutable strings.
>
>>> A
>
>>> possible workaround, assuming lineArray is mutable, is:
>
>>>
>
>>> theLine = [[[lineArray objectAtIndex:i] mutableCopy] autorelease];
>
>>>
>
>>> //then change theLine, then
>
>>>
>
>>> [lineArray replaceObjectAtIndex:i withObject:theLine];
>
>>>
>
>>> You could also reverse the last two steps. Or you could create you
>
>>> could
>
>>> just put mutable strings in your array in the first place.
>
>>>
>
>>> Jonathan
>
>>> _______________________________________________
>
>>> 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.
>
>> _______________________________________________
>
>> 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.
>
_______________________________________________
>
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.
_______________________________________________
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.