3

In an Objective-C class I have a @private ivar that uses an enum of the form:

typedef NS_ENUM(NSInteger, PlayerStateType) {
    PlayerStateOff,
    PlayerStatePlaying,
    PlayerStatePaused
};

However, I include this definition in the header file of that class (since it's used in it). This effectively makes the type public, which isn't what I intended. How can I make this enum type private?

Mr. Smith
  • 4,288
  • 7
  • 40
  • 82
  • How about adding this in it's .m class file? – iDev Nov 19 '12 at 06:45
  • If it's in the .m class file, won't the header not know the type of `PlayerStateType`? – Mr. Smith Nov 19 '12 at 06:54
  • I am assuming that you are not using this in any other classes, so in that case, why dont you declare your private variable in your .m class. – iDev Nov 19 '12 at 06:55

2 Answers2

6

Adding my comment as an answer.

You can add this in your .m class so that while importing it is not shared with other classes. You can just add it below your import statements. If the params of this type are used only in this .m class, you can declare that also in this .m file.

Your .m class will look like,

typedef NS_ENUM(NSInteger, PlayerStateType) {
    PlayerStateOff,
    PlayerStatePlaying,
    PlayerStatePaused
};

@interface ViewController () //Use an extension like this in .m class

@property (nonatomic) PlayerStateType param;

@end
iDev
  • 23,310
  • 7
  • 60
  • 85
  • 2
    Ahh, ty. I didn't know multiple files could add to `@interface ViewController()` ivars. I already add methods, both in categories and without categories, but didn't know ivars worked). I guess ivars just don't work if added from a category. – Mr. Smith Nov 19 '12 at 07:04
  • 1
    @Mr.Smith That's correct, you can add only to extensions and not to categories. – iDev Nov 19 '12 at 07:09
  • Note that you can just declare your instance variables directly in your @implementation - no need to use a class extension just for that. – Wade Tregaskis Nov 19 '12 at 07:26
1

Define it in .m file & declare your privare ivar in controller category in .m file. To know about controller category take a look at Difference between @interface definition in .h and .m file.

Community
  • 1
  • 1
Rahul Wakade
  • 4,765
  • 2
  • 23
  • 25
  • I don't think a class category can add instance variables. I might be misinterpreting what you're saying though, can you be a bit more specific? – Mr. Smith Nov 19 '12 at 06:53
  • 1
    @Mr.Smith, you are correct. A category cannot have instance variables. But an extension can have. Check this http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocCategories.html – iDev Nov 19 '12 at 07:02
  • May be my answer is not clear as I confused myself in category & extention. But I wanted to write same that ACB answered. +1 for ACB – Rahul Wakade Nov 19 '12 at 07:15
  • @Rahul, +1.. You are correct, if you meant extensions and not categories. :) – iDev Nov 19 '12 at 07:16