What is @dynamic in iPad/iPhone
I am jus wondering开发者_如何学Go what is mean by @dynamic in objective-c and how it works.any help please
@dynamic means that you will provide an implementation of those methods dynamically at run time.
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtDynamicResolution.html
has all the details, but basically using @dynamic means that you promise to provide implementations for the property promised methods at runtime.
In particular look here;
http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSObject_Class/Reference/Reference.html
for an example of how you'd construct your dynamic method and place it into the runtime.
Core Data uses this mechanism to provide the accessors. It's quite amazingly cool, once you dig into it :)
And as a side note, meta-programming in ObjC is not for the faint of heart, don't ship it till you grok it, otherwise your users will suffer.
I am quoting Apple's book The Objective-C Programming Language:
@dynamic
You use the @dynamic keyword 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 use it only if you know that the methods will be available at runtime.
You can find a pdf copy here.
Using @dynamic requires that you provide getter/setter methods yourself.
Instead @synthesize creates the getter/setter methods for you.
精彩评论