Re: Coding style question from ObjC newbie
Re: Coding style question from ObjC newbie
- Subject: Re: Coding style question from ObjC newbie
- From: Nicholas Riley <email@hidden>
- Date: Thu, 31 Oct 2002 15:31:58 -0600
- Mail-followup-to: Michael Norris <email@hidden>, email@hidden
On Fri, Nov 01, 2002 at 10:02:24AM +1300, Michael Norris wrote:
>
I'm writing my first Cocoa app which takes a string off the
>
Pasteboard, does some text munging, and puts it back on. I'm looking
>
at using the OFRegularExpression object in the OmniFrameworks to help
>
out with the text munging. I'll need to add my own routine, however,
>
to do a search-and-replace.
>
>
In C, my routine would look something like:
>
>
(void) mungeText(NSString *mungeStr, NSString *findString, NSString
>
*replaceString)
>
{
>
// munging code goes here
>
}
>
>
I was wondering whether in Objective-C it's better to:
>
1) define mungeText as an instance method of some new class of string
Definitely not a good idea. The only reason you'd want to subclass a
string is if you want to add some new kind of string representation.
>
2) define mungeText as an instance method of my main app controller
>
class
Possibly OK if you don't do this too much.
>
3) define mungeText as a new category of NSString
Since mungeText only does something useful for your app (seemingly).
If it's something simple like replacing a string, then perhaps a
category method like:
@implementation NSString (Blah)
- (NSString *)stringByReplacingAllOccurrencesOfString:(NSString
*)findString withString:(NSString *)replacementString;
@end
might work. There are similar methods implemented in OmniFoundation
as categories on NSString.
>
4) just leave mungeText as a C routine in a .c file
Only usually advised if you're writing performance-sensitive code -
for example a lot of the string scanning things in OmniFoundation are
implemented optionally as string functions.
5) would be to create a new StringMunger class, create an instance of
it, and define -munge... as a method of it. That way if you needed to
add some persistent state, or perform multiple transformations on the
same string, or something similar, you'd already have some
infrastructure established.
--
=Nicholas Riley <email@hidden> | <
http://www.uiuc.edu/ph/www/njriley>
Pablo Research Group, Department of Computer Science and
Medical Scholars Program, University of Illinois at Urbana-Champaign
_______________________________________________
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.