Questions 23 to 34 in 50
Page 5
How would you create your own custom view?
By Subclassing the UIView class.
What’s the difference between NSArray and NSMutableArray?
NSArrayʼs contents can not be modified once itʼs been created whereas a NSMutableArray can be modified as needed, i.e items can be
added/removed from it.
What’s the difference between frame and bounds?
The frame of a view is the rectangle, expressed as a location (x,y) and size (width, height) relative to the superview it is contained within. The bounds of a view is a rectangle, expressed as a location (x,y) and size (width, height) relative to its own coordinate system (0,0).
Is a delegate retained?
No, the delegate is never retained ever!
For the same reason, we have to ensure to enclose the delegate protocol calls with appropriate null and method existence checks
For the same reason, we have to ensure to enclose the delegate protocol calls with appropriate null and method existence checks
Outline the class hierarchy for a UIButton until NSObject.
NSObject -> UIResponder -> UIView -> UIControl -> UIButton
If I call performSelector:withObject:afterDelay: – is the object retained?
Yes, the object is retained. It creates a timer that calls a selector on the current threads run loop. It may not be 100% precise time-wise as it attempts to dequeue the message from
the run loop and perform the selector.
Whats the NSCoder class used for?
NSCoder is an abstractClass which represents a stream of data. They are used in Archiving and Unarchiving objects. NSCoder objects are usually used in a method that is being implemented so that the class conforms to the protocol. (which has something like encodeObject and decodeObject methods in them).
Whats an NSOperationQueue and how/would you use it?
The NSOperationQueue class regulates the execution of a set of NSOperation objects. An operation queue is generally used to perform some asynchronous operations on a background thread so as not to block the main thread.
Explain the correct way to manage Outlets memory.
Create them as properties in the header that are retained. In the viewDidUnload set the outlets to nil(i.e self.outlet = nil). Finally in dealloc make sure to release the outlet.
Can you explain what happens when you call autorelease on an object?
When you send an object a autorelease message, its retain count is decremented by 1 at some stage in the future. The object is added to an
autorelease pool on the current thread. The main thread loop creates an autorelease pool at the beginning of the function, and release it
at the end. This establishes a pool for the lifetime of the task. However, this also means that any autoreleased objects created during the
lifetime of the task are not disposed of until the task completes. This may lead to the taskʼs memory footprint increasing unnecessarily.
You can also consider creating pools with a narrower scope or use NSOperationQueue with itʼs own autorelease pool. (Also important – You
only release or autorelease objects you own
When will be the autorelease object released?
Once the pool recives drain message.
When we are implementing our own thread with lot of many autoreleased object, do we need to create autorelease pool? if yes/no why?
Yes , We must create an autorelease pool for the thread as we no longer use the memory allocated to thread post thread execution.
We create pool so all unused instances are cleared when pool recieved a drain call.
only few instances created can be posted to other methods as arguments (or) can be passed to main thread with help of singletons.
only few instances created can be posted to other methods as arguments (or) can be passed to main thread with help of singletons.