• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: static functions in Objective-C
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: static functions in Objective-C


  • Subject: Re: static functions in Objective-C
  • From: Charlton Wilbur <email@hidden>
  • Date: Mon, 29 Aug 2005 19:08:42 -0400


On 29-Aug-05, at 6:24 PM, Bryan Bonczek wrote:

I am looking to write a class that will act as a utility class for simple functions. These functions will make simple "conversions." For instance, a function may take a number 1-10 and convert it to its english equivalent "one"-"ten." Say this method is called convertIntToEnglish, it takes and int and returns and NSString. The method also resides in a class called Util.

Is there a way so that I can use this method like so:
NSString* intEnglish = [Util convertIntToEnglish:1]

If what you're converting is an object, you can do it in a category. Example:


@interface NSNumber (BryanUtil)

- (NSString *) convertIntToEnglish;

@end

@implementation NSNumber (BryanUtil)

- (NSString *) convertIntToEnglish
{
    // not a very good algorithm, but it's short

switch ([self intValue])
{
case 1:
return @"one";
case 2:
return @"two";
/* ... */
case 10:
return @"ten";
default:
// some kind of error logging, or maybe an exception being raised
return @"Error";
}
}


@end

Or you could just do it in a function:

NSString *intToEnglish (int i)
{
switch (i)
{
case 1:
return @"one";
case 2:
return @"two";
/* ... */
case 10:
return @"ten";
default:
// some kind of error logging, or maybe an exception being raised
return @"Error";
}
}


Creating a pseudo-class called Util is not good OO design. Can you say "A util object is...." or "a util object does...." or "a util object represents...."? In languages that enforce an OO orthodoxy, there's no way to have a function except to create this sort of pseudo-class, but Objective-C is not one of those languages.

Charlton




-- Charlton Wilbur email@hidden email@hidden


_______________________________________________ Do not post admin requests to the list. They will be ignored. Cocoa-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
References: 
 >static functions in Objective-C (From: Bryan Bonczek <email@hidden>)
 >Re: static functions in Objective-C (From: Cameron Hayne <email@hidden>)

  • Prev by Date: Re: static functions in Objective-C
  • Next by Date: Re: Spotlight importer and document package question
  • Previous by thread: Re: static functions in Objective-C
  • Next by thread: Re: static functions in Objective-C
  • Index(es):
    • Date
    • Thread