-1

i can't understand how make a variable private in Object-C, in Java i can do it in this way:

public class Counter {
private int cont;

public Counter(){
    cont = 0;
}
public Counter(int v){
    cont = v; }

public void setValue(int v){
    cont = v;
}
public void inc(){
    cont++; }

public int getValue(){
    return cont;
}
}

and then:

public class MyClass extends Counter {

public static void main(String[] args) {

    Counter myC = new Counter();

    System.out.println(myC.getValue());
    //System.out.println(myC.cont); don't work because it's private
}

}

so i can't access at the variable myC.cont because obviously it's private, in Object-C i make the same thing but don't work:

@interface Counter : NSObject {
@private int count;
}

- (id)initWithCount:(int)value;
- (void)setCount:(int)value;
- (void)inc;
-(int)getValueCount;

@end

#import "Counter.h"

@implementation Counter

-(id)init {

count = 0;

return self;
}

-(id)initWithCount:(int)value {

self = [super init];

[self setCount:value];

return self;

}

- (void)setCount:(int)value {

count = value;
}

- (void)inc {

count++;
}

-(int)getValueCount {

return count;
}

@end

and then call it from the main.m:

#import "Counter.h"

int main(int argc, const char * argv[])
{

@autoreleasepool {

    NSLog(@"Hello, World!");
    Counter *myC = [[Counter alloc] init];
    [myC inc];
    [myC inc];
    [myC inc];

    myC.count = 1;
    NSLog(@"%d",myC.getValueCount); //it's 1 instead of 3


}
return 0;
}

i can't understand i can access at the count variable, how i can make it private like in java?

Piero
  • 9,173
  • 18
  • 90
  • 160
  • 1
    This was very well answered in another question: [http://stackoverflow.com/questions/1262463/how-to-make-a-real-private-instance-variable][1] [1]: http://stackoverflow.com/questions/1262463/how-to-make-a-real-private-instance-variable – ahillman3 Oct 24 '12 at 15:26
  • have you read the code in my question? i already do @private but don't works! – Piero Oct 24 '12 at 15:27
  • Sorry. Glad I'm not the only one that did that. I'll let @sergio continue. – ahillman3 Oct 24 '12 at 15:40
  • Did _you_ read the other answer? `myC.count` isn't a variable access, it's sending a message. – jscs Oct 24 '12 at 16:01

2 Answers2

1

myC.count is not accessing your variable, its accessing the method you defined -(int)count;. Because myC is a pointer, of type Counter, you access its member variables directly by dereferencing the pointer like so myC->count. This is not advisable however. Obj-C has built in functionality for generating getters and setters with the @property keyword.

But a break point in `-(int)count and watch as that method gets invoked.

Kyle
  • 434
  • 2
  • 10
0

The best way is using a class extension and defining your private variable in the .m file:

MyClass.h

@interface MyClass
   <PUBLIC DECLARATIONS, variables and methods>
@end

MyClass.m

@interface MyClass ()
@property (nonatomic,...) int count;
...
- (void)privateMethod:(...;
@end

@implementation MyClass
@synthesize count;
...
- (void)privateMethod:(...) {

}
...
@end
sergio
  • 68,819
  • 11
  • 102
  • 123
  • have you read the code in my question? i already do @private but don't works! – Piero Oct 24 '12 at 15:28
  • ok, i already know that way of do it, but my question is why don't work with the @private? :) – Piero Oct 24 '12 at 15:29
  • aren't you even getting a warning on that line? previous versions of GCC would just issue a warning... you may also have the warning disabled... – sergio Oct 24 '12 at 15:32
  • no i don't have any warning, and i have create a new project, so i don't think the warning are disabled... – Piero Oct 24 '12 at 15:33
  • i have tried you solution, but i can access anyway at the count variable from another class... – Piero Oct 24 '12 at 15:46
  • have you removed `count` from your `.h` file? have you cleaned the project and built from scratch? if `count` is only in the `.m`, you cannot access it from outside it... – sergio Oct 24 '12 at 15:55
  • thank you very much for the help, but @Kyle it's right! your answer is however very usefull... – Piero Oct 24 '12 at 15:59