Skip navigation.
 
mlRe: Coding style question from ObjC newbie
FROM : Nicholas Riley
DATE : Thu Oct 31 22:31:58 2002

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_removed>> | <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_removed>
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

Related mailsAuthorDate
mlCoding style question from ObjC newbie Michael Norris Oct 31, 22:02
mlRe: Coding style question from ObjC newbie Nicholas Riley Oct 31, 22:31
mlRe: Coding style question from ObjC newbie tonycate Nov 1, 04:37
mlRe: Coding style question from ObjC newbie Michael Norris Nov 1, 06:09
mlRE: Coding style question from ObjC newbie Jonathan E. Jackel Nov 1, 15:43