Question

Ok, I have class, that I create for my Core Data

LoginPass.h

Then I have First class

FirstClass.h

And then I need use this classes in SecondClass, where I declare them with @class. Heder file

SecondClass.h
...
@class FirstClass;
@class LoginPass;
...
    @interface SecondClass : UIViewController
  {
   ......
  }
@property (strong, nonatomic) FirstClass *fromFirstClass;
@property (strong, nonatomic) LoginPass *myEntity;
...
@end

And in .m file

#import "SecondClass.h"
#import "FirstClass.h"
#import "LoginPass.h"
@implementation SecondClass
...
@synthesize fromFirstClass = _fromFirstClass;
@synthesize myEntity = _myEntity;
...

Ok, I can make some mistakes in code, sry for it. I really don't know and now don't interesting why I need write

 @synthesize myEntity = _myEntity;

but not

 @synthesize myEntity;

But I have another question. Why I can use then in my code

 self.fromFirstClass

But I cann't use

 self.myEntity

Xcode give me an error and say me that I should use

 self._myEntity

What the difference? Why I can use self.fromFirstClass but not self.myEntity? @end

Was it helpful?

Solution 2

@synthesize fromFirstClass = _fromFirstClass;
@synthesize myEntity = _myEntity;

These above lines are correct but nowadays you are not required to synthesize.@synthesize is put by compiler itself.

When you use self.prop you mean you are accessing property.

When you use _prop you call property directly.

EDIT:

When you use self.prop you call the method depending on lhs or rhs of =(assignment) :

-(NSString *)prop; //gets called when you use myName=self.prop;

and/or

-(void)setProp; //gets called when you use self.prop=@"master";

On the other side, if you try to use self._myEntity then it will look for method name having _ which is not there, resulting in Error.

OTHER TIPS

You are confusing instance variables which are variables parts of an object structure, and properties which are really methods to set and get a value.

When you declare @property (strong, nonatomic) FirstClass *fromFirstClass; you actually declare two methods - (FirstClass *)fromFirstClass and - (void)setFromFirstClass:(FirstClass *)aFirstClass.

When you use the dot syntax FirstClass *classA = self.fromFirstClass;, you actually call a method, it is equivalent to FirstClass *classA = [self fromFirstClass];. In the same way, if you write self.fromFirstClass = classB;, you actually call: [self setFromFirstClass:classB];.

If you use the name of an instance variable directly inside an object method, you access this variable.

Now, when you write @synthesize fromFirstClass; in the modern runtime, you let the compiler create a instance variable with the same name fromFirstClass and write the two methods - (FirstClass *)fromFirstClass and - (void)setFromFirstClass:(FirstClass *)aFirstClass that will get and set the instance variable.

If you write @synthesize fromFirstClass = _fromFirstClass;, the same thing happens, except the name of the instance variable which is created has an underscore in front of it.

Finally, in the more recent versions of the compiler, if you don't write anything, the default behavior is to @synthesize fromFirstClass = _fromFirstClass automatically for you.

The compiler would add

@synthesize myEntity = _myEntity;

if you omit the @synthesize totally.

However, you could use as well

@synthesize myEntity;

The key difference is that in the first case, your local variable is called _myEntity while the getter is myEntity and the setter is setMyEntity. So from external you would access yourObject.myEntity either for setting or getting the value. The compiler will take care, that the setter and getter is called. You do not access the prperty directly. [yourObject.myEntity = value] ist identical to [yourObject setMyEntity:value] as well as value = yourObject.myEntity is identical to value = [yourObject myEntity].

So far from accessing properties or their getter and setter from outside. From inside your class you may think that self.myEntity = value is identical to myEntity = value (for the second case). But it is NOT. self.myEntity calls the setter (or getter). This is important especially for the getter becaus that comes with important memory management for free - with or without ARC. While myEntity = value directly accesses the property.

And here comes the _ and its key advantage (imho). If you use the _ notation then the property is called _myValue. Doing so it is explicitely clear for you and the readers of your code when the actual property is accessed directly and when the getter and setter are used.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top