0

I have declared private variables in implementation file of cycles.

myclass.m

@interface myclass()

@property (nonatomic) unsigned int number;

@end

Well, when I put in main.m something like this:

myclass * some = [[myclass alloc] init];

[some setNumber:10]; // no visible @interface for 'myclass' declares the selector 'setNumber'.

unsigned int a=[some getNumber]; //no visible @interface  ...

Every answer on StackOverflow.com points the same as I do. What is the problem?

Ron
  • 1,249
  • 9
  • 20
vladimir
  • 11
  • 2

2 Answers2

0

The problem is that the property number is a private property because you declared it in the interface section in your .m file. Declare it in your header myclass.h files interface block instead.. A private property, ivar, or method is one which can only be accessed from within an instance of your myClass object by using the self keyword (which is basically a pointer to the myClass instance within which you are currently executing).

Moving your property declaration to the header file will make the property accessible from outside the class instance. In other words, it makes it public.


If you want to restrict access to the data the property points to, define the property in the header file as a readonly property:

MyClass.h

@interface MyClass : NSObject

    @property (nonatomic, readonly) unsigned int number;

@end

Then in your .m file create a private ivar manually and override the getter for the number property as follows:

MyClass.m

@implementation MyClass {
    //Instance variable (ivar)
    unsigned int _number;   
}

unsigned int number()
{
    return _number;
}

@end

That way a subclass (or anyone else for that matter) can't muck with the value of number. You can then access number in your main function as follows:

MyClass* myClass = [[MyClass alloc] init];
unsigned int newNumber = myClass.number; //There is no such selector called getNumber BTW, only number.
Ron
  • 1,249
  • 9
  • 20
  • Hmm, but that way all subclasses will have access to that private variable. I want only instances of myclass to have access. – vladimir Jan 21 '15 at 01:12
  • Updated answer... Is this kinda what you are looking for? – Ron Jan 21 '15 at 01:32
  • But I dont want it to be **PUBLIC**. I want it to be **private**. could you rewrite my code so I can see what you suggest. I dont understand. – vladimir Jan 21 '15 at 01:38
  • @vladimir Are you creating a static or dynamic (.so) library? – Ron Jan 21 '15 at 01:48
  • @vladimir - sorry but you will have to expose a public property, or method to achieve your goal. – Ron Jan 21 '15 at 01:55
  • It wont work this way. "Use of undeclared identifier '_number'; did you mean 'number'. " error – vladimir Jan 21 '15 at 01:59
  • I just learn Objective-C. I have over 10 years C++ expirience. – vladimir Jan 21 '15 at 02:10
  • I have been tried like you wrote but "Use of undeclared identifier '_number'..." error appears – vladimir Jan 21 '15 at 02:34
  • you mean self._number? You can only access the _number private ivar from inside the the MyClass instance.. You must access via the self pointer. – Ron Jan 21 '15 at 02:44
  • I will try again all you suggest and read Objective-C big Nerd Ranch Guide further. When I get what I want I will come back here with the answer. – vladimir Jan 21 '15 at 02:59
0

You can use readonly/readwrite property definitions to achieve this.

In MyClass.h

@interface MyClass : NSObject

@property (nonatomic, readonly) NSNumber *number;

@end

In MyClass.m

@interface MyClass ()

@property (nonatomic, readwrite) NSNumber *number;

@end

You can now read the number property outside of the class, and you will only be able to change it inside of MyClass.m. You can set it's value like so, self.number = 5 inside of MyClass.m.

Objective-C does not have the same type of private variables that a language like Java has. What is it that you are trying to accomplish with a private variable?

timgcarlson
  • 3,017
  • 25
  • 52
  • Is there standard way for instance to access private member without declaring it at .h file? – vladimir Jan 21 '15 at 02:02
  • I want to hide private variable from inheritance. EXAMPLE. Class Employee have accessCode. But class Person who is derived from class Employee cant have access to accessCode. I hope you understand me. – vladimir Jan 21 '15 at 02:06
  • I think you have this backwards, Employee should be a subclass of Person... since an employee is first a person and then an employee. Create a `Person` class, and then create an `Employee` class. `Person` will not have access to any of `Employee`'s properties. – timgcarlson Jan 21 '15 at 02:24
  • Yes, I know that, but what if it must be like this. I learn Objective-C. I am C++ programmer and at C++ it is easy to hide inheritance but I would like to know how to do it with Objective-C without overriding methods, and without finding "other ways". I was hope there is one simple way that can be done like this. – vladimir Jan 21 '15 at 02:31
  • You need to expose the property in the .h file in order to access it outside of the implementation file. The other way to access the property declared in your implementation file would be to create instance methods in the header file of your class. – timgcarlson Jan 21 '15 at 02:35
  • But when I create instance methods in the header files, than derived classes have access again. I tried it right now. – vladimir Jan 21 '15 at 02:44