1

I have been looking through sample code provided by Apple and stumbled upon something interesting I've not learned about. In a subclass in the .m file, there are multiple sections declared with @implementation. The last one is the usual @implementation I expect - the class implementation where all the code goes, but above that are a couple short implementations that each define a single method. They are declared in this format: @implementation ClassName (Convenience). The method defined within is in the same format as other methods, but they are prefixed with aapl_. After each @implementation definition follows @end, of course. The way these methods are called is to create an instance of ClassName then call the method as you would with the standard API: [anInstance aapl_thisInterestingMethod:aParameter];

So this appears to be a way to add a method to a class without creating a category, you call it with an instance of the class, and it's only specific to this file. You can use self to refer to the calling argument.

My question are:

  • Is there more to it than that?
  • Why is (Convenience) appended to the end?
  • Is the aapl_ prefix added simply only to indicate this isn't part of the standard API but instead our own method?
  • Why is this better than defining a method as you normally would, accepting in the calling argument as a regular argument? For example: [self anInterestingMethod:myInstance withParameter:aParameter];
  • What are these types of methods/implementations called and what else should I know about them?

EDIT: This question is not at all related to the question this is marked as a duplicate of... @interface is not @implementation

Jordan H
  • 52,571
  • 37
  • 201
  • 351
  • [Categories](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/CustomizingExistingClasses/CustomizingExistingClasses.html) do have some weird uses. But recent versions of the IDE no longer require you to declare methods in a header or even at all, in order to implement them. Any method implemented in the same file you invoke it from will be recognized. Maybe that's all they're doing in this case, just implementing methods and categories without declaring them. – Justin Johns Jun 25 '14 at 21:13
  • Regarding your note about the duplicate: you can't have an `@implementation` block without an `@interface` block. You're still asking about categories even though you've highlighted the former element. If you have more specific questions after actually reading the other question, please feel free to ask them. "I read question NNNN and I don't understand why it says X, because Y should be true." or the like generally makes a good post. – jscs Jun 25 '14 at 21:46
  • @JoshCaswell I understand what `@interface` is and what it means in .h and .m files. My questions are specific to this syntax: `@implementation...@end`, not `@interface` private categories. The specific questions are bulleted. – Jordan H Jun 25 '14 at 21:55
  • You can't have `@implementation Foo (Bar)` without `@interface Foo (Bar)` preceding it, so there's no sense in talking about the first without the second. See also http://stackoverflow.com/questions/360968/category-usage-in-objective-c/ and http://stackoverflow.com/questions/4844040/what-does-interface-ditableviewcontroller-private-mean – jscs Jun 25 '14 at 22:03

0 Answers0