可以不申报ivar但是用于合成? [英] feasible to not declare ivar but use in synthesize?
问题描述
我正在查看一些示例代码,我对缺乏特定ivar的声明感到困惑。希望有人可以帮助我更好地理解这一点:
I'm looking at some sample code and I'm puzzled over the lack of declaration of a specific ivar. Hoping someone can help me understand this better:
typedef NSUInteger (^NumberOfItemsInSection)(ViewClass *viewClass, NSUInteger section);
// class declaration
@interface SampleScrollView : UIScrollView
@property (nonatomic, copy) NumberOfItemsInSection itemsSectionBlock;
@end
// class implementation
@implementation SampleScrollView
@synthesize itemsSectionBlock = _itemsSectionBlock;
- (void)setItemsSectionBlock:(NumberOfItemsInSection)itemsSectionBlock
{
// _itemsSectionBlock is not declared any where in the class
// How does the compiler not complain?
_itemsSectionBlock = [itemsSectionBlock copy];
[self reloadData];
}
@end
实例变量_itemsSectionBlock ,不会在任何地方声明,它只能在属性的setter覆盖中使用。这是如何工作的?
The instance variable, "_itemsSectionBlock", is not declared any where and it can just be used in the property's setter override. How does that work?
推荐答案
它是现代运行时的一部分,并减少了代码的重复 - 声明iVars然后声明这些iVars的属性。
It's part of the modern runtime, and cuts down on the duplication of code - declaring iVars and then declaring properties for those iVars.
它是由 @synthesize
现代运行时允许你做其他以前不能做的事情。例如,您现在可以在.m文件中声明iVars作为类扩展的一部分,这会减少您在公共接口中公开的信息量。
The modern runtime lets you do other things that you thought you couldn't do before. For example, you can now declare iVars in the .m file as part of a class extension, which reduces the amount of information you expose in your public interface.
更新
现代LLVM 4编译器甚至可以让您取消@sytnthesize行。如果您声明一个属性,它将为您自动合成,它甚至会创建一个带有前导下划线的后备存储。
The modern LLVM 4 compiler even lets you do away with the @sytnthesize line. If you declare a property it will auto-synthesize for you and it will even create a backing store with a leading underscore.
这篇关于可以不申报ivar但是用于合成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!