0

I'm looking at a .h file for an Objective-C class and I see multiple interface declarations in it and I am unsure as to what the differences are and what they mean.

First I see

@interface TAModel : NSObject

Which I recognize. TAModel is the class and NSObject is it's super class. What I'm confused about is further down I see another interface declaration:

@interface TAModel (Protected)

Also inside another .m file (unrelated to the first two) I have seen:

@interface TAWorker (Private)

I was wondering what the second two mean, what they are doing. As far as I know with objective-c there is no true protected visibility between classes.

strikerdude10
  • 283
  • 3
  • 9

1 Answers1

0

It's creating a category class in which they're putting their protected/private members. The usual idiom is just to create a class extension (so you'll often see @interface Foo (); the difference here is that you can also declare more fields, not just properties and methods) in the .m file.

Neither way of doing this truly protected or private as you can still technically get at the things declared there by casting to id first or through one of the performSelector: methods. But it's pseudo-private because you don't publish the interface publicly if it's not in the .h file.

ahruss
  • 2,070
  • 16
  • 21
  • 1
    `@interface Foo ()` defines a class *extension*, not a category. – Martin R Aug 13 '14 at 20:51
  • Ok that makes sense. The private and protected keywords in the parentheses don't actually make them private/protected, its more the fact that they are in the .h .m files right? Is this an advisable way to go about this or you think there are better ways? – strikerdude10 Aug 13 '14 at 21:10
  • @strikerdude10 Right. What word you put in the parens makes no difference; it's just where you put the declaration. Generally I think the class extension is a better way to go, as it gives you more flexibility with what you can do. There's nothing wrong with using categories, though. – ahruss Aug 13 '14 at 21:13