What does #dynamic do in Objective-C? [duplicate] - objective-c

This question already has answers here:
#synthesize vs #dynamic, what are the differences?
(8 answers)
Closed 8 years ago.
Objective-C has a feature called #dynamic.
Google only lists results about dynamic typing.
I rarely see this in code and I don't understand what it is used for. Can anyone explain me this? Thanks.

#dynamic means “my class will figure out how to respond to this at runtime.” Uses a runtime mechanism for an object to intercept messages it normally wouldn’t respond to. In the case where a Core Data db is used to store persistent data, NSManagedObject turns these into calls to -valueForKey: and -setValueForKey:.
Take a look at Lecture 12 (Fall 2010) of Stanford's iPhone development course.

Related

Objective-C >> Is There a Way to Look at an Object Retain Count Table "on the Run"? [duplicate]

This question already has answers here:
How do I verify reference count in ARC mode?
(7 answers)
Closed 9 years ago.
While using ARC, life is much easier in terms of memory management; but, let's say I wish to look at a certain object while the app is running and see how many pointers are pointing to it at each certain point in the code. Is there a way to do that?
You can access the retain count using -retainCount or CFGetRetainCount but it will almost never give you any meaningful or useful information. Objects can be added to autorelease pools, retained by the various internals of the objective-c runtime or Apple frameworks, ARC, etc. You shouldn't really care how many people have retained an object, only whether you need a strong reference to it at any point in time.
Relevant link: whentouseretaincount.com

Is it necessary, or even good practice, to create properties for all iVars? [duplicate]

This question already has answers here:
Is there any reason to declare ivars if you're using properties exclusively in Objective-C?
(4 answers)
Closed 8 years ago.
If an ivar is to be used globally within the class, but will never be accessed by other classes, are we still supposed to use properties?
It is generally a good idea, as the generated accessors will take care of things like memory management and KVO for you. You can put the property in a class extension so other classes can't use it.
For me, it depends on what the instance variable will be used for.
If it's an object representing some data, then I will always use a property.
If it's just a simple BOOL for some internal bookkeeping by a couple of methods in the class, then I won't create a property for it.

iOS: Initialise object at start of application for all controllers to use [duplicate]

This question already has answers here:
Where and how should I instantiate an object which will be used globally in an IOS app?
(3 answers)
Closed 8 years ago.
i'm working on a little iOS project, and i stumbled across a problem with variable scoping.
what i need is an object that is initialised at launchtime, and is available to all controllers until the application closes.
the object will hold data that is loaded either from a database (sql) or from local storage - im not 100% sure yet what to do here.
i need all viewControllers to access that data-holding object at all times, and i need the object to retain when the app enters the background.
is this possible to achieve? and if, then how would i do it?
for simple variables i know i can use extern variables, but does it also work for complete objects?
thanks for an answer,
sebastian
This is one of the more common questions here. I would advise to stay away from extern variables and singletons, see my answer for this related question and this sample Xcode project for a better solution. (The sample project is very bare-bones at the moment, I will add more common scenarios later.)
A possible point of initialization could be your app delegate's didFinishLaunching:withOptions: method, or would that be too late? You could also reference the data via the app delegate (like [[UIApplication sharedApplication] delegate]). Edit: Just to be clear, one can do, but I would not recommend storing and accessing arbitrary data this way.
You can also reference objects using external references, as in extern NSString *gGlobalString;. You need a safe place for initialization, though. A singleton could be a better solution.

Use-case of +new in Cocoa? And why it exist? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
alloc, init, and new in Objective-C
There is +alloc/-init... and +new. Their role are almost same in documentation except +new is designed for instance pooling. (according to my understanding :)
However the pooling is possible with +alloc/-init. Why the separated method is required? Or is there any reason for the method?
And I couldn't find any example utilizes the method. When should I use this method? Can I get some use-case of the method +new?
In the original Objective-C implementation from Stepstone, +new was what we used to create instances. It was a holdover from Smalltalk. NeXT separated +new into +alloc/-init since most classes don't have to do anything different to allocate the memory their instances use, and it didn't make sense to duplicate that code all over the place.

What is mean by delegate? Why we need it? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Delegates, can't get my head around them
Hi friends,
What is mean by delegate in objective C? Why we need it? When should we use it? Is there any types in it? How to use it?
Please friends, use simple words and examples to explain. I saw so many articles, forums.. But still i can not catch the exact explanation of it..
Thank you
A delegate is a way to modify the behavior of a class without requiring the class to be subclassed. Often you don't want to dramatically change behavior, but tweak it a bit; subclassing would be overkill, so that's where delegates come in to play.
Look at it this way: a teenager represents a class, and her parent the delegate. The teenager's friend calls her to come hang out at the mall, but the teenager has to ask her parents if it's okay first. The parent -- the delegate -- can say yes or no. That's how delegates work in Cocoa.
Is there any types in it?
Delegates can generally be of any type. In 10.6, many delegates implement protocols with optional methods, so you'll see types like id <BlahClassDelegate>, but that wasn't common before 10.6.