ios开发基础之@dynamic关键字的作用

@dynamic

  You use the@dynamickeyword to tell the compiler that you will fulfill the API contract implied by a property either by providing method implementations directly or at runtime using other mechanisms such as dynamic loading of code or dynamic method resolution. It suppresses the warnings that the compiler would otherwise generate if it can"t find suitable implementations. You should only use it if you know that the methods will be available at runtime.

  The example shown inListing 5-3illustrates using@dynamicwith a subclass ofNSManagedObject.

  Listing 5-3 Using @dynamic with NSManagedObject

  @interface MyClass : NSManagedObject

  {

  }

  @property(nonatomic, retain) NSString *value;

  @end

  @implementation MyClass

  @dynamic value;

  @end

  NSManagedObjectis provided by the Core Data framework. A managed object class has a corresponding schema that defines attributes and relationships for the class; at runtime, the Core Data framework generates accessor methods for these as necessary. You therefore typically declare properties for the attributes and relationships, but you don"t have to implement the accessor methods yourself, and shouldn"t ask the compiler to do so. If you just declared the property without providing any implementation, however, the compiler would generate a warning. Using@dynamicsuppresses the warning.

  大概的翻译一下:

  @dynamic 就是要来告诉编译器,代码中用@dynamic修饰的属性,其getter和setter方法会在程序运行的时候或者用其他方式动态绑定,以便让编译器通过编译。其主要的作用就是用在NSManageObject对象的属性声明上,由于此类对象的属性一般是从Core Data的属性中生成的,Core Data框架会在程序运行的时候为此类属性生成getter和Setter方法。


转载:http://roll.sohu.com/20110121/n302359581.shtml

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值