What does the text inside parentheses in @interface and @implementation directives mean?
Asked Answered
R

2

27

I've got a very basic question about some sample code from Apple. In the .m file, the class declarations look like this:

@interface MyMovieViewController (OverlayView)
[...]
@end

@interface MyMovieViewController (ViewController)
[...]
@end

@implementation MyMovieViewController(ViewController)
[...]
@end

@implementation MyMovieViewController (OverlayView)
[...]
@end

@implementation MyMovieViewController
[...]
@end

Full code here.

It seems like the stuff inside parentheses ("OverlayView" and "ViewController") are just there to help break up the code and make it more readable, but don't actually impact the execution of the code. But I don't want to be misunderstanding something important, so I thought I'd check to make sure.

Is my understanding right? Thanks!

Romanesque answered 11/9, 2011 at 13:25 Comment(0)
A
24

Those are called Categories and allow you to add further functionality to your classes.

A category allows you to add methods to an existing class—even to one for which you do not have the source. Categories are a powerful feature that allows you to extend the functionality of existing classes without subclassing. Using categories, you can also distribute the implementation of your own classes among several files. Class extensions are similar, but allow additional required APIs to be declared for a class in locations other than within the primary class @interface block.

From the Apple docs on Categories and Extensions.

Ac answered 11/9, 2011 at 13:32 Comment(0)
E
5

Those are "categories".

With those you can extend any objective-C class by adding methods, which will apply to all the objects of the class.

More detailed article here: http://macdevelopertips.com/objective-c/objective-c-categories.html

Ellersick answered 11/9, 2011 at 13:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.