11

I am jus wondering what is mean by @dynamic in objective-c and how it works.any help please

3 Answers 3

13

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

6

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.

0
2

Using @dynamic requires that you provide getter/setter methods yourself.

Instead @synthesize creates the getter/setter methods for you.

1
  • 2
    Some classes will generate methods automatically, such as NSManagedObject. Mar 25, 2011 at 13:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.