I’m finding it difficult to understand when it is necessary to create class methods. From what I’ve read, they are important for creating new objects, but I do not see how. The following class create a simple shape black rectangle. Can anyone show me how to incorporate a class method to do something that I could not do with an instance method?
Shape.h
#import <UIKit/UIKit.h>
@interface Shape : UIView;
- (id) initWithX: (int)xVal andY: (int)yVal;
@end
Shape.m
#import "Shape.h"
@implementation Shape
- (id) initWithX:(int )xVal andY:(int)yVal {
self = [super init];
UIView *shape = [[UIView alloc] initWithFrame:CGRectMake(xVal, yVal, 10, 10)];
shape.backgroundColor = [UIColor blackColor];
[self addSubview:shape];
return self;
}
@end
Class methods have several uses:
As you’ve heard, they’re essential for creating new objects. Creating objects is a two step process: first, you allocate memory for the object using the
+allocclass method, then you initialize the object using some instance method that usually begins with-init. The+allocmethod must be a class method because you don’t have an instance upon which to call it; you only know the class name. The-initmethod should be an instance method, because+allocreturns an instance, an you want to initialize the properties unique to that instance, not to the entire class. So, you used a class method then an instance method in your code above:UIView *shape = [[UIView alloc] initWithFrame:CGRectMake(xVal, yVal, 10, 10)];Class methods are useful for retrieving singletons, or shared instances of a class. For example, the
NSUserDefaultsclass stores preferences and settings associated with your app. In most cases, people like to use a single set of preferences for their entire app, so it has a class method+standardUserDefaultsthat returns a single instance ofNSUserDefaultsthat everyone can use at any point in the app’s lifetime to get all the preferences associated with an app. Without that singleton and class method, you’d have to create and pass around an instance ofNSUserDefaultsthroughout your app, which gets messy. Other examples include+[UIDevice currentDevice]and+[NSFileManager defaultManager].Class methods are great for returning data or properties shared by all instances of the class. For example,
UIViewhas a class method+layerClassthat returns the kind of layer the view hosts. Since all instances of a particularUIViewsubclass use the same kind of layer, it has nothing to do with individual instances and thus makes sense as a class method.You’ll see a lot of class methods called “convenience methods.” These methods (generally) combine calls to
+allocand-initand return an autoreleased instance of a class. For example, you can save some typing by writing[NSArray arrayWithObjects:...]instead of[[[NSArray alloc] initWithObjects:...] autorelease]. If you’re using ARC, these convenience methods are less useful, but they are still everywhere in Cocoa.Class methods are also sometimes used for utility classes, or classes that don’t have state, and just have a bunch of methods that take in all the parameters they need. For example, you might have a method like
+[PhoneUtilities parseAreaCodeFromPhoneNumber:string]. There are not many of these methods built-in to Cocoa; some people argue that it’s better to make them regular C functions, singletons, or regular objective-c classes with instance methods.I’m sure there are more, but that should give you some ideas.