Objective-C is a dynamic language. The idea of ββwhat an implementation is is different from a static language.
For the most part, this is the code that most of us implement inside the @implementation ... @end block.
But what if the method is not found? Then the object has a chance to link it dynamically.
Imagine you have an interface for a sound effect player:
@protocol FX - (void)playBeep; - (void)playSiren; - (void)playHonk; @end
An implementation may have Beep.mp3, Siren.mp3, Honk.mp3 files for playback, but instead of implementing each of the methods, it can override -forwardInvocation: and parse the selector string, something like this pseudocode:
NSString *selName = NSStringFromSelector([invocation selector]); if ([selName startsWith:@"play"]) { NSString filename = fileNameFromSelector(selName); [self playSoundFileNamed:filename]; }
This may seem far-fetched, but as soon as you start using the dynamic functions of the language, you will begin to find more and more places where it makes sense. And in terms of meaning, I mean, does this help in the long run?
In the above case, just add the method name -sound * to the interface and run the corresponding sound file. It just works.
Another example from personal experimentation is how to handle Core Data objects in a more natural way. I want to do this: NSArray * people = [Person findAllWithNameLike: @ "B%"]; instead of dropping predicates, requesting queries, etc.
But I do not want to define every method permutation in the code.
How about if I wanted to build an XML builder? I would look at a dynamic approach. He served Groovy builders well (see Groovy / Grails examples).
The last example: I have a system of signs where I can define behavior in the form of groups of methods and I have objects that assimilate this behavior. Thus, although the compiler does not see the implementation of the interface to which my object corresponds, the implementation is injected into it from the attribute class using the Objective-C runtime. Why should I do this? I find that many delegate methods are boiler plates, but at the same time, one base class for each situation is not flexible enough. Instead of cutting and pasting from code samples, my "samples" are compiled and launched :) and any changes are reflected in all projects using the attribute.
To understand why all this is available to you, you should play with the Smalltalk environment (look for Pharo or Squeak). Objective-C has its roots here.
And finally, to stop these warnings:
#pragma clang diagnostic push #pragma clang diagnostic ignored "-Wprotocol" @implementation ... @end #pragma clang diagnostic pop