"Instance" mean in Objective-C? Kindly tell me where to use Class Method And where to use Instance Method,also tell me where we use (Instacetype) method? why/where we use multi Parameters?
-
Simply how did you create the object ? NSObject *obj = [[NSObject alloc]init]; In this alloc is the class method and init is the instance method ...Arun– Arun2015-10-26 08:09:21 +00:00Commented Oct 26, 2015 at 8:09
-
Directly we are calling through the class name is class method and some restriction are there pls refer this link stackoverflow.com/questions/1053592/… class methods- it is just like a static methodArun– Arun2015-10-26 08:15:57 +00:00Commented Oct 26, 2015 at 8:15
-
This is too broad for stackoverflow, which is about answering programming questions, not to teach computer science. Buy a book or join a class.trojanfoe– trojanfoe2015-10-26 08:25:00 +00:00Commented Oct 26, 2015 at 8:25
-
Hope this helps you: Purpose of Instance Methods vs. Class Methods in Objective-C ,soumya– soumya2015-10-26 08:48:58 +00:00Commented Oct 26, 2015 at 8:48
1 Answer
A class method is a method whose self parameter is a reference to the class's class object.
An instance method is a method whose self parameter is a reference to a specific instance of the class.
Those are the technical differences.
A more practical answer is that an instance method operates on a single instance of the class, while a class method operates at a more global, non-specific level. A class method can act as a factory method, such as NSString's stringWithFormat: method. It can also be used to configure behavior that will affect all instances of the class. It can also be used to operate on a collection of instances of the class, such as sorting or filtering.
instancetype is a keyword that can be used as a placeholder for the current class's type. It says to the compiler: pretend that I wrote <my class name> here, so if you see the result of this method assigned somewhere, you know what type it's supposed to be.