Search code examples
iphoneobjective-ciossynthesize

@class @synthesize useablility


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


Solution

  • @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.