关于@synthesize和@dynamic的区别
来源:互联网 发布:断电mysql修复 编辑:程序博客网 时间:2024/06/05 23:41
There are a lot of answers, but also a big confusion. I’ll try to put some order (or increase the mess, we’ll see…)
Let’s stop talking about Xcode. Xcode is an IDE. clang is a compiler. This feature we are discussing is called autosynthesis of properties and it’s an Objective-C language extension supported by clang, which is the default compiler used by Xcode.
Just to make it clear, if you switch to gcc in Xcode, you won’t benefit from this feature (regardless from the Xcode version.) In the same way if you use a text editor and compile using clang from the command line, you will.Thank to autosynthesis you don’t need to explicitly synthesize the property as it will be automatically synthesized by the compiler as
@synthesize propertyName = _propertyName
However, a few exceptions exist:
readwrite property with custom getter and setter
when providing both a getter and setter custom implementation, the property won’t be automatically synthesizedreadonly property with custom getter
when providing a custom getter implementation for a readonly property, this won’t be automatically synthesized@dynamic
when using @dynamic propertyName, the property won’t be automatically synthesized (pretty obvious, since @dynamic and @synthesize are mutually exclusive)properties declared in a @protocol
when conforming to a protocol, any property the protocol defines won’t be automatically synthesizedproperties declared in a category
this is a case in which the @synthesize directive is not automatically inserted by the compiler, but this properties cannot be manually synthesized either. While categories can declare properties, they cannot be synthesized at all, since categories cannot create ivars. For the sake of completeness, I’ll add that’s it’s still possible to fake the property synthesis using the Objective-C runtime.overridden properties (new since clang-600.0.51, shipping with Xcode 6, thanks Marc Schlüpmann)
when you override a property of a superclass, you must explicitly synthesize it
It’s worth noting that synthesizing a property automatically synthesize the backing ivar, so if the property synthesis is missing, the ivar will be missing too, unless explicitly declared.
Except for the last three cases, the general philosophy is that whenever you manually specify all the information about a property (by implementing all the accessor methods or using @dynamic) the compiler will assume you want full control over the property and it will disable the autosynthesis on it.
A part from the cases I listed above, the only other use of an explicit @synthesize would be to specify a different ivar name. However conventions are important, so my advice is to always use the default naming.
转自:http://stackoverflow.com/questions/19784454/when-should-i-use-synthesize-explicitly
- 关于@synthesize和@dynamic的区别
- @synthesize和@dynamic的区别
- @synthesize和@dynamic的区别
- @synthesize和@dynamic区别
- @synthesize和@dynamic区别
- @synthesize和@dynamic区别
- @synthesize和@dynamic区别
- @synthesize和@dynamic区别
- @synthesize和@dynamic区别
- @synthesize和@dynamic区别
- @synthesize和@dynamic区别
- @synthesize和@dynamic区别
- @synthesize和@dynamic区别
- @synthesize和@dynamic区别
- @synthesize和@dynamic区别
- @synthesize和@dynamic区别
- iOS之 @synthesize和@dynamic的区别
- 关于@synthesize 和 @dynamic 关键字
- 九度oj 1069
- velocity第三个应用例子--遍历集合/数组
- input框 长度每变化一次触发一个事件
- VC6.0下64位整数的操作
- PHP
- 关于@synthesize和@dynamic的区别
- hdoj2027 统计元音
- [基本实验] 使用SQLMap
- 2012年5月SAT香港真题解析
- tomcat中修改server.xml设置虚拟目录后,再删掉那部分内容,虚拟路径还是能访问的。
- Android 客户端Cookie失效的问题
- velocity第四个应用例子---遍历Map集合
- Ios 实现麦克风捕获和AAC编码
- linux 中特殊符号用法详解