@dynamic的使用常见的情况是什么? [英] What is common case for @dynamic usage?

查看:148
本文介绍了@dynamic的使用常见的情况是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以前的帖子关于@synthesize和@动态。

There is previous post about difference of @synthesize and @dynamic.

我想从如何通常使用@dynamic的角度了解更多关于动态的内容。

I wanna to know more about dynamic from the perspective of how to use @dynamic usually.

通常我们使用@dynamic与NSManagedObject

Usually we use @dynamic together with NSManagedObject

// Movie.h
@interface Movie : NSManagedObject {
}
@property (retain) NSString* title;
@end

// Movie.m
@implementation Movie
@dynamic title;
@end

实际上,根据对于编译时间的了解,实际上没有生成getter / setter @dynamic,所以有必要实现你自己的getter / setter。

Actually there are no generated getter/setter during compiler time according to understanding of @dynamic, so it is necessary to implement your own getter/setter.

我的问题是在这个NSManagedObject案例中,超类NSManagedObject中getter / setter的粗略实现是什么?

My question is that in this NSManagedObject case, what is the rough implementation of getter/setter in super class NSManagedObject ?

除了上述情况,还有多少其他情况使用@dynamic?

Except above case, how many other cases to use @dynamic ?

谢谢,

推荐答案

@dynamic 向编译器指示您计划为访问者提供自己的实现,即使编译器当前无法看他们。如果省略 @dynamic ,不要使用 @synthesize ,则会发生以下两种情况之一:

@dynamic indicates to the compiler that you plan to provide your own implementation for the accessor(s), even if the compiler can't currently see them. If you omit @dynamic and don't use @synthesize, one of two things will happen:


  1. 如果您只提供了一半的访问者(例如,一个没有setter的getter在 readwrite 属性),或者你使用GCC,编译器会警告你。

  2. 如果你使用Clang编译你的代码,那么自动生成正确的访问器你(没有正式支持Synthesize-by-default)。

  1. If you've only provided half an accessor (for instance, a getter without a setter on a readwrite property), or you're using GCC, the compiler will warn you.
  2. If you're using Clang to compile your code, proper accessors will be automatically generated for you. (Synthesize-by-default is not officially supported.)

@dynamic 因此有助于防止编译器进行上述任一操作。如果您以非常动态的方式实现属性(如运行时功能),但这很少需要,这也可能会派上用场。

@dynamic is therefore useful to prevent the compiler from doing either of the above. This might also come in handy if you implement a property in a very dynamic way, like with runtime functions, but that's rarely necessary.

这篇关于@dynamic的使用常见的情况是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆