开发者社区 问答 正文

@class @synthesize用法

问题描述:

为了coredata创建类:
LoginPass.h

然后first类
FirstClass.h

然后在secondclass中使用这些类,用@class声明:

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

在.m文件中

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

我不明白为什么要写这行代码:@synthesize myEntity = _myEntity;但是换成这行代码就不行了:@synthesize myEntity;

问题是:为什么可以用self.fromFirstClass,但是不能用self.myEntity。

如果用 self.myEntity的话系统会报错。为什么?

展开
收起
爵霸 2016-03-26 10:23:20 2023 分享 版权
1 条回答
写回答
取消 提交回答
  • @synthesize fromFirstClass = _fromFirstClass;
    @synthesize myEntity = _myEntity;

    这两行代码都正确,不过现在@synthesize 已经包含在编译器里所以不用@

    在使用self.prop用来访问属性

    在用_prop时是直接调用属性

    在使用self.prop时, 调用方法取决于lhs 或者hs of =(assignment) :
    -(NSString *)prop; //在使用myName=self.prop调用;

    and/or
    -(void)setProp; //在使用self.prop=@"master"调用;

    同时 如果使用 self._myEntity ,系统会需找名字中带_的方法, 导致错误.

    2019-07-17 19:15:35
    赞同 展开评论
问答地址: