程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> ios-@class @synthesize用法

ios-@class @synthesize用法

編輯:編程綜合問答
@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的話系統會報錯。為什麼?

最佳回答:


@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 ,系統會需找名字中帶_的方法, 導致錯誤.

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved