Hi again,
Here are some other questions about optimization :
Suppose I have a string with data I want to remove inside it.
I have two solutions :
1) using an NSScanner and copy the data I want into another string.
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">NSMutableString *resultString = [[NSMutableString alloc] init];
NSScanner *originalStringScanner = [[NSScanner alloc] initWithString:originalString];
if ( [originalStringScanner scanUpToString:@"new Array" intoString:NULL] &&
[originalStringScanner scanUpToString:@"(" intoString:NULL] )
{
while ( [originalStringScanner scanUpToString:@"new" intoString:&tempString] )
{
if ( [originalStringScanner isAtEnd] == NO )
{
[resultString appendString:tempString];
if ( [originalStringScanner scanUpToString:@"(" intoString:NULL] )
scanLocation = [originalStringScanner scanLocation];
}
}
[originalStringScanner setScanLocation:scanLocation];
if ( [originalStringScanner scanUpToString:@"

;" intoString:&tempString] )
[resultString appendString:tempString];
}</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">2) using an NSMutableString with deleteCharactersInRange method
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">// here originalString is a NSMutableString
NSScanner *originalStringScanner = [[NSScanner alloc] initWithString:originalString];
char length = (char)[@"new Array" length]; //this is to understand what length is…
NSRange myRange;
while ( [originalStringScanner isAtEnd] == NO )
{
if ([originalStringScanner scanUpToString:@"new Array" intoString:NULL])
{
myRange = NSMakeRange(originalStringScanner scanLocation, length);
[originalString deleteCharactersInRange:myRange];
}
}
// this second search is necessary, but implemented differently, however I am quite sure it is longer to execute since it scan the string from beginning each time…
do
{
myRange = [originalString rangeOfString:@"new Dictionary"];
[originalString deleteCharactersInRange:myRange];
} while (myRange.location != NSNotFound);</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">I suppose the first is faster since it parses the string only once, however I do not know how these methods are implemented, and maybe someone here know some goos details on them.
Thanks