Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
977 views
in Technique[技术] by (71.8m points)

iphone - What is common case for @dynamic usage?

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

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

Usually we use @dynamic together with NSManagedObject

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

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

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.

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

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

Thanks,

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

@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. 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 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...