@synthesize window = _window做什么? [英] What does @synthesize window=_window do?
问题描述
我理解 @synthesize窗口;
结合 @property
'自动创建'你的setter和getter,但是我不确定当你分配一个像
@synthesize searchBar = _searchBar;
这是否意味着我可以简单地在我的方法中使用_searchBar而不是说self.searchBar?
是否可以防止使用此委托方法冲突ivar名称:
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
它是相当于 self.searchBar
而不是 searchBar
还是这两个相同?
您的属性几乎总是有一个支持变量。什么
@synthesize searchBar = _searchBar;
确实声明搜索栏的支持变量将被称为 _searchBar
。这允许您将属性的名称与变量的名称分离。事实上,如果你不使用 @synthesize
,你根本不需要一个支持变量。
<对于为什么人们这样做,每个人都有不同的理由。就个人而言,我这样做
- 避免与变量名称发生冲突和
- 当我说清楚时当我使用实例变量时,我正在使用局部变量。
I understand that @synthesize window;
combined with @property
'auto-creates' your setters and getters, but I'm not sure exactly what happens when you assign a value like
@synthesize searchBar = _searchBar;
Does this mean that I can simply use _searchBar instead in my methods rather than say self.searchBar ?
Is it to prevent a clash of ivar names for instance with this delegate method:
- (void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
Is it the equivalent of self.searchBar
rather than searchBar
or are those two identical anyway?
Your properties almost always have a backing variable. What
@synthesize searchBar = _searchBar;
does is declare that the backing variable for your search bar will be called _searchBar
. This allows you to decouple the name of the property from the name of your variable. In fact, if you don't use @synthesize
you don't need to have a backing variable at all.
As for why people do this, everyone has different reasons. Personally, I do it to
- avoid clashes with variable names and
- make it clear when I'm using a local variable and when I'm using an instance variable.
这篇关于@synthesize window = _window做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!