Sun*_*day 31 objective-c llvm synthesize
在什么条件下,在Objective-c中自动@synthesize?
也许在使用LLVM 3.0及更高版本时?从阅读网络看起来似乎@synthesize是不必要的从Xcode 4开始.但是当我没有@synthesize属性时,我正在使用Xcode 4并收到警告.
对于为什么不自动合成属性的一些响应似乎暗示@synthesize在某些情况下可以在某些时候被省略.
另一个(旧的)参考暗示@synthesize可能在将来的某个时候是自动的.
Quu*_*one 24
从clang 3.2(大约2012年2月)开始,默认情况下提供Objective-C属性的"默认合成"(或"自动属性合成").它基本上如您最初阅读的博客文章中所述:http://www.mcubedsw.com/blog/index.php/site/comments/new_objective-c_features/(该帖子将该功能描述为"已启用,然后已禁用" ";我不知道这是否是Xcode的问题,或者clang开发者本身是否在这个问题上来回走动).
据我所知,在clang 3.2中不会默认合成属性的唯一情况是这些属性是从协议继承的.这是一个例子:
#import <Foundation/Foundation.h>
@protocol P
@property int finicky;
@end
@interface A : NSObject <P>
@property int easygoing;
@end
@implementation A
@end
int main() { A *a = [A new]; a.easygoing = 0; a.finicky = 1; }
Run Code Online (Sandbox Code Playgroud)
如果您编译此示例,您将收到警告:
test.m:11:17: warning: auto property synthesis will not synthesize property
declared in a protocol [-Wobjc-protocol-property-synthesis]
@implementation A
^
test.m:4:15: note: property declared here
@property int finicky;
^
1 warning generated.
Run Code Online (Sandbox Code Playgroud)
如果你运行它,你将从运行时收到一个错误:
objc[45820]: A: Does not recognize selector forward:: (while forwarding setFinicky:)
Illegal instruction: 4
Run Code Online (Sandbox Code Playgroud)
rob*_*off 13
从Xcode 4.4开始,如果你不写@synthesize或@dynamic属性.编译器就像你写的一样@synthesize property = _property.
在Xcode 4.4之前,您必须为每个属性执行以下操作之一,否则编译器将发出警告,您将收到运行时错误.在Xcode 4.4或更高版本中,您可以执行以下任何操作,而不是让编译器自动合成属性访问器和实例变量.
@synthesize指令.@dynamic指令并以某种方式在运行时提供属性getter和(如果需要)setter.readwrite,则显示属性setter方法.请注意,您可以用@synthesize指令(或@dynamic指令),并同时明确提供的getter和/或setter方法.但@synthesize如果省略它们,请提供它们.
此外,如果您手动实现了setter AND getter,则generateize将不会自动进行.因此,如果你想知道为什么你不能访问_someVariable,声明@property(...)SomeType someVariable,那是因为你已经实现了setSomeVariable:和someVariable方法.
| 归档时间: |
|
| 查看次数: |
12247 次 |
| 最近记录: |