views:

154

answers:

3

Hi all,

I have a few variables in my iPhone app as class instance variables (which they need to be because they are used within multiple methods of that class.

However, I don't want them to be exposed to the parent class. I have seen the @private keyword, but I am not sure if this is the appropriate use or not.

Does anyone how to keep instance variables as private to the class?

This is probably an Objective-C question as well, and not necessarily specific to the iphone.

Many thanks, Brett

+1  A: 

@private makes your intention clear and is the way to go.

Edit:

@interface YourClass : NSObject {
@private
    int privateData;
@public
    int publicData;
}

// method declarations....

@end
Eiko
I haven't been able to find out how to to implement @private though. Any ideas? Where do I put it for a set of variables? How wold I separate those out from the ones I would like to make public? Many thanks!
Brett
Edited with an example.
Eiko
Can this work with class methods also? Ie. @private -(void)method1;
Brett
@private can't be used for any variables with class-wide scope (e.g. a single common memory location accessible from any object in the class).
hotpaw2
@hotpaw2: Did you downvote? The question was about *instance* variables - mistakenly called "class instance" variables.
Eiko
@Brett: No, only ivars can have private access. Methods (or properties for that matter) can be accessed by everybody.
Eiko
I'll agree the original question is unclear, since the words "class" and "instance" both appear several times. However @Brett later added a comment/question about class methods, which don't have instance variables. Thus my answer about using static globals as class-method usable variables.
hotpaw2
@Brett: If you don't declare a method in the class's interface .h file, then the use of these methods outside of that class will cause compiler warnings (sort-of-like declaring your intent that they be private); however you can still ignore warnings, call them, or even look them up by name, from anywhere.
hotpaw2
A: 

The @private directive means that the instance variable is accessible only within the class that declares it. If you want a subclass to have access to the instance variables, use the @protected directive. I'm not exactly sure what you mean when you say that you don't want your instance variables exposed to the parent class, but I'm pretty sure that @private is what you want.

You use the directives like this:

@interface MyClass : NSObject {
@private
    float privateFloat;
    float anotherPrivateFloat;
@protected
    float protectedFloat;
@public
    float publicFloat;
}

The default scope for instance variables is protected.

James Huddleston
Perfect! Thanks everyone!
Brett
How about for methods? Is there any way to keep them private by using the @private directive?
Brett
Not that I know of. You can hide method declarations using a category with an empty name to declare a "private API," but as far as I know, the privacy is not enforced. This is why you hear about iPhone apps getting rejected for called Apple's "private" methods. Check out this question: http://stackoverflow.com/questions/172598/best-way-to-define-private-methods-for-a-class-in-objective-c
James Huddleston
A: 

Class instance variables (assuming you mean a variable that can be set in one object and read in another object of the same class) in Objective C are the same as static global variables in plain ANSI C (assuming you put one class implementation inside one .m file).

Static globals won't be exposed to any other class (super, sub, alien, etc.) or code outside the implementation file containing these class instance variables.

The @private declaration stuff in other answers here is most probably incorrect (depending on what you mean by your terminology), as those instance variables won't have class-wide scope, but are private to each individual object (unless exposed by a property).

hotpaw2
Brett asked about a variable he can use in several methods - not to be shared between multiple instances. Also, there is not really a "class variable" thing in Obj-C (at least they are not the same level compared to class methods).
Eiko