0

I know the difference between class methods and object methods but I am not clear for why we use class methods in iOS? can anyone give me example so I can understand that..??

user7388
  • 1,741
  • 2
  • 19
  • 25
  • 1
    Its completely asinine this was downvoted despite being the top result on google and obviously very helpful to many devs – ChuckKelly Mar 13 '16 at 04:14
  • @ChuckKelly agreed. Maybe a better way to rephrase the question would be: what are the advantages of using/creating class methods vs. instance methods? – alexisSchreier Jan 28 '17 at 22:29

5 Answers5

1

Class methods are methods the class handles and instance methods are methods on the individual instances.

A common use-case for class methods is a convenience allocator like NSString's +stringWithFormat:. It's identical (with an added autorelease, if you're not using ARC) to using [[NSString alloc] initWithFormat:] but saves you a lot of time.

Another example is MFMailComposeViewController's +canSendMail method. It's unnecessary to create an instance first just to see whether sending mails is supported. The class knows better about this and therefore this method is a class method instead of an instance method.

Similar question

Community
  • 1
  • 1
Fabian Kreiser
  • 8,307
  • 1
  • 34
  • 60
1

Stackoverflow is your friend.

What is the difference between class and instance methods?

Class methods are usually convenience methods such as NSString's +stringWithFormat: That is, you don't need to instantiate a class to use them.

Community
  • 1
  • 1
Snowcrash
  • 80,579
  • 89
  • 266
  • 376
1

Class method and Object method.

@interface TheClassA : NSObject
+ (TypeA)classMethod;
- (TypeB)objectMethod;
@end

@implementation TheClassA

+ (TypeA)classMethod
{
}

- (TypeB)objectMethod
{
}

@end



main()
{
   [TheClassA classMethod]; // Correct!!!
   [TheClassA objectMethod]; // Wrong!!!

   TheClassA *obj = [[TheClassA alloc] init];
   [obj classMethod]; // Wrong;
   [obj objectMethod]; // Correct;
   [obj.class classMethod]; // Correct
   [obj.class objectMethod]; // Wrong
}

Class method has the scope like generic C-Language function. It is global method. But... Object method give the effective only the object... Class method can't change it's class properties, can't send messages to the object. But object method can send messages to the object and can change properties.

Good luck.

booiljoung
  • 776
  • 6
  • 10
1

I often use class methods to create a program which uses a design pattern called 'singleton'. If I have for example a class which handles requests from other classes, I only need 1 instantiation of the request handling class and I want my entire program to use the same one. In the request handling class I put a static variable which will hold the instantiation.

static MyRequestHandler *requestHandler;

And use a class method to get it, if it didn't exist yet, I make it first and then save it.

+ (MyRequestHandler *) getRequestHandler {
     if (requestHandler == nil) {
         requestHandler = [[MyRequestHandler alloc]init];
     }
     return requestHandler;
}

Other classes can reach this instantiation in there code like this

MyRequestHandler *requestHandler = [MyRequestHandler getRequestHandler];
Priyal
  • 879
  • 1
  • 9
  • 30
Pieter Gunst
  • 139
  • 5
0

Class methods are used for library routines. To access these methods one do not have to create its object.

For example, Some times it gives you easy initializers to create an object.

NSArray *arr = [NSArray array];
Apurv
  • 17,116
  • 8
  • 51
  • 67