Mutable != Mutable ?
Mutable != Mutable ?
- Subject: Mutable != Mutable ?
- From: Michael Hanna <email@hidden>
- Date: Mon, 4 Aug 2003 11:33:28 -0400
From my program I get:
2003-08-04 11:18:54.643 Country Song[22148] Attempt to mutate immutable
NSString with replaceCharactersInRange:withString:
however the data structure I'm copying from is an NSMutableArray and
the String is an NSMutableString.
the program replaces tokens '%s' (maybe a bad choice) in an
NSMutableString with much longer phrases from an NSMutableArray.
So I'm not sure why I'm getting this error. Is it because the token and
the replacement phrases are different lengths?
Michael
///////////////////////
//CODE CSController.m//
- (IBAction)generate:(id)sender
{
NSString *phrasesPath; // the path to the bundled phrases property
list
NSString *linesPath; // the path to the bundled lines plist
NSString *key = @"lines"; // key to get lineArray from the
Nictionary
unsigned i; // an index value for the for-loop, increments
through lineArray
unsigned phraseNum = 0; // a value that increments through the 12
phrases in phraseArray
NSString *keyName;
NSString *tokenString = @"%s"; // the 12 tokens to be replaced
within the while-loop
NSMutableString *theLine;
[....]
theLine = [lineArray objectAtIndex:i]; // retrieve a line
NSRange whereFound = [theLine rangeOfString: tokenString]; // find the
first '%s'
// this following while loop replaces the %s tokens with their
corresponding
// randomly chosen replacement
// while there are still %s tokens in the line, loop
while(whereFound.location != NSNotFound)
{
// now that we have a token to replace, load the phraseArray at
index
// phraseNum to retrieve a phrase to replace the token with
phraseArray = [phraseDict objectForKey:[keyArray
objectAtIndex:phraseNum]];
// using the custom random function, randPhrase, select a phrase
from phraseArray
// and replace the token
/* PROGRAM CRASHES ON THIS LINE */
[theLine replaceCharactersInRange: whereFound withString:
[phraseArray objectAtIndex: randPhrase([phraseArray count])]];
NSLog(@"theLine: %@", theLine);
whereFound = [theLine rangeOfString: tokenString];
phraseNum++;
} // while
[....]
//CODE CSController.m//
///////////////////////
from CSController.h :
[....]
NSDictionary *phraseDict; // the sum total of all the possible
phrases
NSDictionary *lineDict; // the lines
NSMutableArray *phraseArray; // an array of phrases for temporary
storage
NSMutableArray *lineArray; // the array of lines
NSMutableArray *theSong; // the song lyrics in it's final form,
ready for o/p
NSArray *keyArray; // the list of keys in the phrasesDict, in
lex-order
[....]
_______________________________________________
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.