开发者

How to set up non-instantiated classes in Objective-C (Classes with just methods)

I'm looking to开发者_Go百科 create a class in Objective-C for an iOS project that is focused on fetching data. I'm familiar with how classes normally work, setter and getter methods and variables. However, for this class since it's only performing a function (returning NSMutableArrays) I don't want to have to create an instance of the class to use the methods inside the class.

Any idea how I can do this neatly and efficiently?


This is a little bit atypical in Objective-C. Since classes in Objective-C can't actually have state beyond what is available to ordinary functions (i.e. there are no class variables), a class that's never instantiated is relatively useless in most cases. The normal design patterns for this kind of functionality are:

  • A singleton class (if you need lots of state)

  • A set of functions (if you don't)


You want to make class methods?

@interface Foo : NSObject {}
+(NSMutableArray*)someClassMethod:(id)params;
@end

...

@implementation Foo
+(NSMutableArray*)someClassMethod:(id)params {
   // whatever implementation 
   return nil;
}
@end

...

NSMutableArray* array = [Foo someClassMethod:nil];


If you're only performing functions, and you don't need to support subclassing etc, why not just write them as C functions rather than a class with methods?


If this is just a class that performs some functions, you could write it as a C function.

In your header file --

NSMutableArray *functionThatReturnsMutableArray(NSObject *param1, NSString *param2);

In your implementation file --

NSMutableArray *functionThatReturnsMutableArray(NSObject *param1, NSString *param2)
{
   ...
   return aMutableArray;
}

And that just include the .h file in your class that needs these functions and call them directly.

NSMutableArray *anArray = functionThatReturnsMutableArray(param1, param2);


Depending on what you are doing (the same NSString operations, UIView manipulations, etc), you could implement a category (I answered a question yesterday with the explanation below -- copied for your convenience ;).

Categories extend an existing class with additional methods or with your version of existing methods. For example, let's say you want to add a method that returns the first letter of a string to NSString. To do this you would create a category as follows:

Interface - JULString.h

#import NSString

@interface NSString (JULString)

-(NSString *) firstLetter;

@end

Implementation - The typical convention is that the filename of the category is the name of the class you are extending followed by “+” and the name of the category. In this case the file would be called NSString+JULString.m

#import "NSString+JULString.h"

@implementation NSString ( JULString )

- (NSString *)firstLetter
{
  return [NSString stringWithFormat:@"%C", [self characterAtIndex:1]];
}
@end

The neat thing about categories is that now they extend the behavior of ANY instance of the class you are working with. In other words, any NSString in your application will have your new methods (provided that you import the proper header file of course). Beware though, as with great power comes great responsibility. Overwriting class using a category behaviors may lead to undesired effects, so be cautious.

A couple of links you may want to check are:

  • Apple's guide to Objective-C
  • Learn Objective-C

Note: I don't have my Mac with me so I'm writing this code basically off the top of my head (and using some code from the sites above as a reminder). So I apologize in advance for any mistakes ;)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜