Re: NSString category question
Re: NSString category question
- Subject: Re: NSString category question
- From: publiclook <email@hidden>
- Date: Wed, 5 Mar 2003 21:34:27 -0500
You seem to understand the difference between an immutable string and a
mutable string because you are using both in you method.
Why do you expect that returning a string instance from a method is
going to change the contents of an immutable string ?
On Wednesday, March 5, 2003, at 08:34 PM, Koen van der Drift wrote:
Hi,
I made an NSString category that removes all white spaces from a
string. My
understanding is that a category is an extension of an existing class,
so I
assumed I can use it as follows:
NSString* myString = [myTextView string]; // or some other
initialization
[myString removeWhiteSpace];
However, this doesn't change myString, it only works if I do:
myString = [myString removeWhiteSpace];
Maybe I misunderstand something about categories, or is the way it is
supposed to work?
Here's the code in the category:
-(NSString*) removeWhiteSpace
{
NSCharacterSet *whitespaceSet;
NSMutableString *strippedString;
NSScanner *scanner;
NSString *temp;
whitespaceSet = [NSCharacterSet whitespaceAndNewlineCharacterSet];
strippedString = [[NSMutableString alloc] init];
scanner = [NSScanner scannerWithString:self];
while( [scanner scanUpToCharactersFromSet:whitespaceSet
intoString:&temp] )
{
[strippedString appendString:temp];
}
return [strippedString autorelease];
}
thanks,
- Koen.
_______________________________________________
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.