在Xcode 4中创建新项目时,样板代码在将实现文件中的ivars合成为时,会添加下划线字符:
@synthesize window = _window;
Run Code Online (Sandbox Code Playgroud)
要么:
@synthesize managedObjectContext = __managedObjectContext;
Run Code Online (Sandbox Code Playgroud)
有人能告诉我这里完成了什么吗?我不是一个完整的润滑剂,但这是客观的一个方面 - 我不明白.
另一个困惑点; 在app委托实现中,在如上所述合成窗口iVar之后,在应用程序didFinishLaunchingWithOptions:方法中,使用self引用窗口和viewController ivars:
self.window.rootViewController = self.viewController
[self.window makeKeyAndVisible];
Run Code Online (Sandbox Code Playgroud)
但是在dealloc方法中它是_window或_viewController
谢谢
我知道@synthesize window;结合@property'自动创建'你的setter和getter,但我不确定当你分配一个像这样的值时会发生什么
@synthesize searchBar = _searchBar;
Run Code Online (Sandbox Code Playgroud)
这是否意味着我可以简单地在我的方法中使用_searchBar而不是说self.searchBar?
是否使用此委托方法防止ivar名称的冲突:
- (void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
Run Code Online (Sandbox Code Playgroud)
它是否相当于self.searchBar而不是searchBar或者那两个相同?
我在其实现文件中为类定义了一个属性.
@property (nonatomic, strong) IBOutlet UIView *headerView;
Run Code Online (Sandbox Code Playgroud)
然后我重写其getter函数,-(UIView*)headerView(){...}从主bundle获取一些资源.
在下面的代码中,我需要在加载资源后将属性"headerView"设置为其他视图的子视图.这是代码无法加载资源.
[self.tableView setTableHeaderView:_headerView];
Run Code Online (Sandbox Code Playgroud)
不调用Getter函数.我将属性变量更改为self.headerView:
[self.tableView setTableHeaderView:self.headerView];
Run Code Online (Sandbox Code Playgroud)
它现在有效......
self.xxx和_xxx之间有什么区别吗?我认为它们与不同方面的财产相同.