Re: Adding methods to NSString
Re: Adding methods to NSString
- Subject: Re: Adding methods to NSString
- From: Jörn Salewski <email@hidden>
- Date: Thu, 27 Nov 2003 13:37:57 +0100
am 27.11.2003 9:54 Uhr schrieb tony cate unter email@hidden:
Just a few thoughts on the code you provide:
1. I would expect a method with the name 'removeCharacter' to belong to an
NSMutableString and returning void.
Should a method returning an NSString not rather being something like
-(NSString *)stringByRemovingCharacter...?
And should the parameter passed in not better be of type unichar? Also
characterAtIndex returns an unichar.
2. And maybe one could shorten the code a little bit:
@implementation NSString (NSStringExtensions)
-(NSString *)stringByRemovingCharacter:(unichar)thisChar {
int index;
NSMutableString* tempString = [NSMutableString stringWithString:self];
index = -1;
while(++index < [tempString length]) {
if([tempString characterAtIndex:index] == thisChar){
[tempString deleteCharactersInRange:NSMakeRange(index,1)];
}
}
// if not simply returning the tempString:
return [NSString stringWithString:tempString];
}
Cheers,
Joern Salewski
>
In the category method, are you referring to the string as 'self', as
>
in [self doSomething]?
>
>
Here's a sample category that works for me:
>
>
@implementation NSString (NSStringExtensions)
>
-(NSString *)removeCharacter:(char)thisChar
>
{
>
int index, length;
>
NSMutableString* tempString = [[[NSMutableString alloc] init]
>
autorelease];
>
>
[tempString setString:self];
>
length = [tempString length];
>
index = -1;
>
>
while(++index < length){
>
if([tempString characterAtIndex:index] == thisChar){
>
[tempString deleteCharactersInRange:NSMakeRange(index,1)];
>
length = [tempString length];
>
}
>
}
>
return [NSString stringWithString:tempString];
>
}
>
>
On Nov 26, 2003, at 4:53 PM, Frank wrote:
>
>
> Hi,
>
>
>
> I tried adding a category to NSString class to add a method which
>
> check if one string is contained within another (like contains
>
> statement in applescript).
>
>
>
> Problem is that the NSString object complains it doesn't recongnize
>
> the selector. Also the NSString object presents itself as being of the
>
> NSCFString class, which I believe is the problem. As I cannot find any
>
> reference to the NSCFString class (header files?) I cannot add a
>
> category to it.
>
>
>
> Is there any way out of this impasse (or am I just overlooking the
>
> obvious)?
>
>
>
> Regards,
>
>
>
> Frank
>
> _______________________________________________
>
> 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.