• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Mutable != Mutable ?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Mutable != Mutable ?


  • Subject: Re: Mutable != Mutable ?
  • From: "seaside.ki" <email@hidden>
  • Date: Mon, 4 Aug 2003 17:56:43 +0200

Michael,

from the documentation:

>>
Replaces the characters from aRange with those in aString. aString may not be nil. Raises an NSRangeException if any part of aRange lies beyond the end of the receiver.
<<<

So your receiver string theLine should be large enough to hold all content. Something like this should be
fine:

NSString *newLine = [NSMutableString stringWithFormat:@"%@%@%@",
FirstPartOf( theLine ),
[phraseArray objectAtIndex: randPhrase([phraseArray count])],
LastPartOf( theLine )
];
[theLine release];
theLine = newLine;

Stefan

Am Montag, 04.08.03 um 17:33 Uhr schrieb Michael Hanna:

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.


__________
Mit freundlichen Gr|_en | Best regards
turingart, branch of CUBiC
http://www.turingart.com/
Home of seeCard Rendezvous : http://www.turingart.com/seeCard.htm
Current release: 1.0.1 - minor changes, feedback form
_______________________________________________
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.

References: 
 >Mutable != Mutable ? (From: Michael Hanna <email@hidden>)

  • Prev by Date: Mutable != Mutable ?
  • Next by Date: RE: Mutable != Mutable ?
  • Previous by thread: Mutable != Mutable ?
  • Next by thread: RE: Mutable != Mutable ?
  • Index(es):
    • Date
    • Thread