-4

I want to know what is need of class type method. Whenever this question is asked people compare between Class type method and Instance method. when we should use Class type method and what is benefit of Class type method over Instance methods ? It will be great if anybody answer me. Thanks in advance.

  • You don't need an instance to call a class method and you can share things between instances. That's basically it. In Obj-C a class method is actually a type of instance method but understing that is a bit more advanced. – Sulthan Sep 25 '15 at 09:51
  • Class method should have nothing to do with anyone specific instance. – KudoCC Sep 25 '15 at 09:53
  • @MuneeshChauhan check below given answer and if not get properly than go through this link http://stackoverflow.com/questions/1053592/what-is-the-difference-between-class-and-instance-methods – JAY RAPARKA Sep 25 '15 at 10:21
  • @Sulthan, in ObjC a class method is an instance method of the related class' class. However, this implementation detail of the ObjC runtime is a bit academic as it has no impact on a programmers' day to day work. To us it is simply a class method like static ones in other languages. – Hermann Klecker Sep 25 '15 at 10:29
  • @hermann-klecker not true. Its not an implementation detail, its part of the language specification. Class method is not a static method. One of the differences is the fact that class methods can be overriden. you can also use self in class methods. There is a conceptual difference between languages like Swift, Objc, Ruby and languages like Java or C++. – Sulthan Sep 25 '15 at 12:00

1 Answers1

1

Here is the difference between class and instance method.

Class Method

  • You can use class method for your common functionality like validation,color from hex color,etc.. and used anywhere in project without creating instance of that class (called like utility class).

  • Class method indirectly said static method.

  • No need to create object of class for calling this type of method.

  • Class method starts with + in objective-c and class func in swift.

  • stringWithFormat is a class method of NSString class you can call directly using class name (not required to create object of NSString).

    int no = 5;
    NSString *str = [NSString stringWithFormat:@"Some String %d",no];
    

Instance Method

  • You create instance method for your specific functionality like setupView,etc.. and you have to create instance for calling it.

  • Instance method is a simple method within specific class.

  • Need to create object of class for calling this type of method.

  • Instance method starts with - in objective-c and func in swift.

  • isEqualToString is an instance method of NSString class you can call only using with NSString class object.

    NSString *str = @"some string";
    NSString *str2 = @"some string";
    Bool isEqual = [str isEqualToString:str2];
    
Sulthan
  • 128,090
  • 22
  • 218
  • 270
JAY RAPARKA
  • 1,353
  • 2
  • 13
  • 33