@synthesize window = _window做什么? [英] What does @synthesize window=_window do?

查看:211
本文介绍了@synthesize window = _window做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解 @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 ,你根本不需要一个支持变量。



<对于为什么人们这样做,每个人都有不同的理由。就个人而言,我这样做


  1. 避免与变量名称发生冲突和

  2. 当我说清楚时当我使用实例变量时,我正在使用局部变量。


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

  1. avoid clashes with variable names and
  2. make it clear when I'm using a local variable and when I'm using an instance variable.

这篇关于@synthesize window = _window做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆