Why do the courses at Stanford use the lazy initialisation? [duplicate] - objective-c

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.

Related

Is enumerateObjectsUsingBlock: faster than a for-in loop? Why? [duplicate]

This question already has answers here:
Objective-C enumerateObjectsUsingBlock vs fast enumeration?
(2 answers)
Closed 8 years ago.
I was reading the NSHipster article on enumeration, which claims that for-in loops are faster than enumerateObjectsUsingBlock::
Unless you actually need the numerical index while iterating, it's almost always faster to use a for/in NSFastEnumeration loop instead.
This answer provides some rebuttal for that quote:
Fast enumeration requires translation from an internal representation to the representation for fast enumeration. There is overhead therein. Block-based enumeration allows the collection class to enumerate contents as quickly as the fastest traversal of the native storage format.
What is the translation process to move from the internal representation to the representation for fast enumeration? I understand that there is some overhead there, but how much?
The real answer: No difference that matters to pretty much any real world program. and Don't worry about it until you find an actual issue during performance quantification. and If the speed of execution of a loop matters, then your overall app architecture is likely the bug.
With that said, there is certainly some academic curiosity worth pursuing.
See:
Objective-C enumerateUsingBlock vs fast enumeration?

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.

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 does #dynamic do in Objective-C? [duplicate]

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.