Class and instance methods with the same name - objective-c

I'm looking at Apple's MVC Networking sample project and I have found that in the class PhotoGallery the author has created instance and class versions of the method abandonGalleryCacheAtPath::
On line 139:
+ (void)abandonGalleryCacheAtPath:(NSString *)galleryCachePath
On line 457:
- (void)abandonGalleryCacheAtPath:(NSString *)galleryCachePath
All the instance version of the method seems to do is a bit of logging before calling the class method as follows:
[[QLog log] logWithFormat:#"gallery %zu abandon '%#'", (size_t) self.sequenceNumber, [galleryCachePath lastPathComponent]];
[[self class] abandonGalleryCacheAtPath:galleryCachePath];
The log message includes self.sequenceNumber, which is an instance variable which would not be available to the class method.
A couple of questions:
Will the system automatically direct calls to the right method e.g. if another class method calls self abandonGalleryCacheAtPath:abc then the class version of the method will be executed, and if another instance method calls it then the instance version of the method will be executed?
Do you think the author has implemented the instance method purely so that the value of sequenceNumber can be logged? Are there any other design/technical benefits of doing this kind of double implementation?

Since Objective C uses dynamic method binding, the system will automatically direct calls to the Class or Instance method depending on the context in which the call was made. Refer Objective-C uses dynamic binding, but how?

Related

Where to Use Class Method ,Instance Method in 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.

When and who called +initialize?

My program has a class and that class has an +initialize method. I wonder who calls that method? The debugging tools are very unclear:
What triggers +initialize to be called? The beginning of application launch?
The runtime sends initialize to each class in a program exactly one time just before the class, or any class that inherits from it, is sent its first message from within the program.
See the documentation for + (void)initialize on NSObject.
An authoritative blog post on the question of initialize states that initialize is executed once when the class is first used, i.e. as the docs state before the class is sent its first message.

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

Objective C and magic methods in class

Does objective-c offer a way to intercept calls to class method that does not exist?
The forwardInvocation method is what you are going to want to use. It is called automatically when a non-existent selector is called on an object. The default behavior of this method is to call doesNotRecognizeSelector:(which is what outputs debug information to your console), but you can override it do anything you want. One recommended approach by Apple is to have this method forward the method invocation to another object.
- (void)forwardInvocation:(NSInvocation *)anInvocation
Note that forwardInvocation is a fairly expensive operation. An NSInvocation object needs to be created by the framework and (optionally) used to invoke a selector on another instance. If you are looking for a (relatively) faster method of detecting non-existent selectors then you can choose to implement forwardingTargetForSelector instead.
- (id)forwardingTargetForSelector:(SEL)aSelector
You should Apple's documentation for how to override these methods effectively, there are some gotcha's to watch out for, particularly when overriding the forwardInvocation method on the same object that will have the missing selectors.
Yes, you can with the resolveClassMethod: class method (which is defined on NSObject):
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html
Here is also something to watch out for (stumped me the first time): http://iphonedevelopment.blogspot.com/2008/08/dynamically-adding-class-objects.html

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.