Ios @综合与使用自我
我是iOS新手,一直在从教程和教授那里学习。Ios @综合与使用自我,ios,properties,Ios,Properties,我是iOS新手,一直在从教程和教授那里学习。 在.h文件中,我们有: @interface ViewController : UIViewController <UITextFieldDelegate> @property (strong, nonatomic) IBOutlet UILabel *myResponse; @property (strong, nonatomic) IBOutlet UITextField *myInput; @end @sysnthesize
在
.h
文件中,我们有:
@interface ViewController : UIViewController <UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet UILabel *myResponse;
@property (strong, nonatomic) IBOutlet UITextField *myInput;
@end
@sysnthesize myResponse
@synthesize myInput
我的问题是:在.m
文件中,添加@synthesis
然后使用[myInput]
等属性与删除@synthesis
并使用[self.myInput]
有什么区别。我在Xcode 5上运行,所以我知道我有自动合成功能,但是两者之间有没有更细微的区别
我的教授使用了@synthesis
,而我下面的教程只是使用了self.propertyName
,因此我很好奇
谢谢。属性不再需要显式使用
@synthesis
。如果你的教授在使用它,那只是为了明确地展示引擎盖下发生的事情
如果在.h文件中定义了属性,则会自动生成访问器、变量和实例变量。如果显式地@synthesis
这些属性,则可以通过指令后提供的任何ivar名称访问它们。如果未使用@synthesis
,则可通过\u somePropertyName
获得自动生成的IVAR
在您的示例中,使用
@synthesis
,可以通过实例变量myResponse
访问myResponse
UILabel。如果没有@synthesis
,可以通过\u myResponse
获得。在这两种情况下,都可以通过self.myResponse
获得。最佳做法是在任何生命周期方法、访问器或变体中使用实例变量(-viewDidLoad
,-viewwillbeen
,-setMyResponse:
等)并在所有其他方法中使用self.propertyName
,以清楚区分类的属性和实例变量。如果您刚刚开始学习,最好阅读苹果文档了解最基本的问题:不要再使用@synthesis