Where to Use Class Method ,Instance Method in Objective-C - objective-c

"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?

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.

Related

What's the difference of instance method and class method in Objective-C? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Objective-C: Class vs Instance Methods?
Objective-C - difference between class method and static method?
In ObjC, A single dash before a method name means it's a instance method. A plus before a method name means it's a class method. but what is the difference in programming?
The difference between a class method and an instance method is that an
instance method requires an instance of the class on which it will
(generally) operate. The message to invoke an instance method must be
sent to an instance of a class.
Probably the most common single use of class methods is object
factories; messages that you send to a class to create an instance
configured according to the parameters you've sent in. For example in
Cocoa the NSString class has several class methods named
stringWithSomethingOrOther: that will create a new NSString object and
hand it back to you.
On the other hand, NSString also has many instance methods -
operations which really have no meaning without an actual instance to
work with. A commonly-used one might be the length method, which tells
you how many characters are in the specific NSString instance to which
the message is sent.
Also see this.
What is the difference between class and instance methods?
An instance method is invoked on objects. A class method is invoked on class.
For example the line:
SomeClass *object = [[SomeClass alloc] init];
Here you can see that the "alloc" works on "SomeClass" and not on "object".
Whereas:
[object callMyFunction]; will act on "object" and not "class". This is an instance method.
The main difference with those two is the former one ie with single dash before it is only called by the instance of that class where it is declared ie one have to create one instance of that class means one object for that class and using . one can call the instance method
In class method, the later one can be called directly using the class name. To call class methods one dosen't need any object.
Please refer this link from apple developers documents

Where should I set default variable values in ObjectiveC class

I can't set default values in the header file, so where should they be set? In the class's designated initialiser?
Yep, if you need specific values, use the init like methods.
This is the recommended way.
The designated initializer is the method in each class that guarantees inherited instance variables are initialized (by sending a message to super to perform an inherited method). It’s also the method that does most of the work, and the one that other initialization methods in the same class invoke. It’s a Cocoa convention that the designated initializer is always the method that allows the most freedom to determine the character of a new instance (usually this is the one with the most parameters, but not always).

Class methods and instance methods - when/when not to use them?

I was wondering when and when not to use class methods and instance methods. I need some practical examples. I am really confused. Another question: can't we do exactly the same things with instance methods that we can with class methods?
Class methods: The method isn't tied to any specific object. In a way it acts like a free function in the class's namespace. No 'self' pointer. For instance [UIScreen mainScreen] is a class method because there's only one screen and there's no need to care about multiple 'screen instances'.
Instance method: Tied to a specific object.
This applies to most OO languages, not just obj-C.
At the implementation level, an instance method call contains a hidden pointer to a data structure (the object), a class method does not.
The practical question to ask is whether your call requires sending the call some specific data which is or could best be encapsulated as instance data inside an object, or not.
You (usually) can do the same thing with class methods as instance methods, but then you have to explicitly pass the object as a visible parameter in the call, which is uglier looking and also potentially disables some method override features of the Objective C language.
Use class methods for utility functions and Instance methods for object oriented stuff.
Eg. For Mathematical calculation (eg sin x ) use class method. But for invoking a behavior specific to an object.. use instance method ..
A class method as the name implies is bounded to the class. You can invoke them just with the name of the particular class. These can be normally exposed methods of a class.
For example
NSArray
+ (id)arrayWithArray:(NSArray *)array;.
You call it with the class name NSArray. What you expect is just a creation of a object of the type of that particular class. This doesn't need an object to invoke. Also these are very basic method required so its better to make it as a class method.
On the other hand instance method as the name implies is very much bound to the instance. Object is an entity that encapsulates state (ivars) and behaviors (methods) of a class. This can be very specific to the object.
For example
- (NSUInteger)count;
Lets take NSArray *a and NSArray *b. If a contains 5 items whereas b contains 4, instance methods called upon these instances will produce different results. And thats why we need instances to be initialized while invoking instance method. They work on the context(or state) of the object they are been called upon. Also they are not exposed as the class methods are.
Hope this helps.
If you want to use instance objet or instance variable you have to go with instance Methods.
Bcz Inside the class you cant access the Instance instance objet or instance variable.
Class methods are static methods.

Objective-C Find all init (constructor methods)

using the "Method * class_copyMethodList(Class cls, unsigned int *outCount)" function one can get a list of all methods that exist on an objective-C class.
I would like to know how to find which of these methods are constructors as I am writing an IOC container. I would like to determine the constructors and their parameter types.
I would like to know how to find which of these methods are
constructors as I am writing an IOC container. I would like to
determine the constructors and their parameter types.
In short, you can't. Or, at the least, you'll find that down this path lies madness.
First, Objective-C does not have constructors. It has initializers, sometimes many, and -- for a properly written class -- only one of which is the designated initializer. There is no way to identify the designated initializer at compile time or run time.
How do I use this with a Method * and no instantiated member of the
class?
You don't. First you allocate an instance of the class, then you initialize the instance.
Overall, this level of abstraction just isn't done in Objective-C outside of academic investigations. It can be done, but it is generally avoided because of the fragility of the resulting solution and the hairball of code-hell that is trying to dynamically support the underlying C ABI (go look at the source to libffi).
If you want to go down this path, then you are far better off either defining a custom abstract class that all of your containers will subclass that can provide the binding logic to the class behind it.
Or use protocols; i.e. a class could implement an IOCBean protocol and one method would be initIOCGoop that is the designated initializer goo.
Doing this generically for all classes is going to be rife with fragility, special cases, and will require a gigantic mess of code that will be difficult to maintain over time.
You can get the method signature by using the following method:
methodSignatureForSelector:
From the documentation:
An NSMethodSignature object records type information for the arguments and return value of a method. It is used to forward messages that the receiving object does not respond to—most notably in the case of distributed objects. You typically create an NSMethodSignature object using NSObject’s methodSignatureForSelector: instance method (on Mac OS X v10.5 and later you can also use signatureWithObjCTypes:). It is then used to create an NSInvocation object, which is passed as the argument to a forwardInvocation: message to send the invocation on to whatever other object can handle the message. In the default case, NSObject invokes doesNotRecognizeSelector:, which raises an exception. For distributed objects, the NSInvocation object is encoded using the information in the NSMethodSignature object and sent to the real object represented by the receiver of the message.

Objective-C: Class vs Instance Methods?

In Objective-C, while creating any class, how do we decide whether we need to mark a method as Class method or Instance Method ?
I know the difference between the 2, but my question is how to decide the marking (+/-) for any method ?
+ denotes a class method, - denotes an instance method. You create class or instance methods where your application needs them. Should you actually know the difference between the two, and your application, then you should have no problems understanding when to use which.
I believe you don't know the differences in how they apply to your application, so here's a small primer:
You use a class method when you need to access some behaviour globally through all instances of that class. i.e., [[self class] someSpecialThing];
You also use a class method when you need a factory method; and
Everywhere else, you use an instance method.