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.
Related
This question already has answers here:
When to use lazy instantiation in iOS?
(4 answers)
Closed 9 years ago.
Why does the course at Stanford use the lazy initialization for all getters?
Is this correct? Does it have any real advantage?
One advantage (for me) is that the init method can become much shorter and you need not check if a variable is allocated.
The idea is to load resources on demand. This way everything loads faster and when needed. In the cases it's not used, it doesn't allocate additional memory.
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
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.
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.
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.