Featured post

Difference between protocol and delegates?

A protocol, declared with the ( @protocol syntax in Objective-C) is used to declare a set of methods that a class "adopts" (de...

Friday, 10 April 2015

Important answer on memeory allocation

What happens when you add autorelease two times?
Answer:

Adding autorelease two times will release the object twice,your app will crash, because you'll be releasing a deallocated object.
 
What is the use of Controller?
Answer:-


In iOS, the controller is generally a subclass of UIViewController that manages a view, it is also responsible for responding to delegation      messages and target-action messages.  


Example:
You have a UITableViewController which is a subclass of                   
  UIViewController hat manages a UITableView  

What is ARC?
Answer:-

Automatic Reference Counting, or ARC was introduced in  iOS 5,is a feature of the new LLVM 3.0 compiler and it completely does away with the manual memory management.you no longer call retain, release and autorelease.With Automatic Reference Counting enabled, the compiler will automatically insert retain, release and autorelease in the correct places in your program ARC is not a runtime feature (except for one small part, the weak pointer system), nor is it *garbage collection* that you may know from other languages.All that ARC does is insert retains and releases into your code when it compiles it.

OR

ARC is a compiler-level feature that simplifies the process of managing the lifetimes of Objective-C objects. Instead of you having to remember when to retain or release an object, ARC evaluates the lifetime requirements of your objects and automatically inserts the appropriate method calls at compile time.  

What is retain ?
Answer:-

it is retained, old value is released and it is assigned -retain specifies the new value should be sent -retain on assignment and the old value sent -release -retain is the same as strong. -apple says if you write retain it will auto converted/work like strong only. -methods like "alloc" include an implicit "retain"  


What is atomic?
Answer:-

-Atomic means only one thread access the variable(static type). -Atomic is thread safe. -but it is slow in performance -atomic is default behaviour -Atomic accessors in a non garbage collected environment (i.e. when using retain/release/autorelease) will use a lock to ensure that another thread doesn't interfere with the correct setting/getting of the value. -it is not actually a keyword.

Example :
@property (retain) NSString *name;
@synthesize name;  

Difference between shallow copy and deep copy?
Answer:-

Shallow copies duplicate as little as possible. A shallow copy of a collection is a copy of the collection structure, not the elements. With a shallow copy, two collections now share the individual elements.
Deep copies duplicate everything. A deep copy of a collection is two collections with all of the elements in the original collection duplicated.
OR
Shallow copy is also known as address copy. In this process you only copy address not actual data while in deep copy you copy data.

Suppose there are two objects A and B. A is pointing to a different array while B is pointing to different array. Now what I will do is following to do shallow copy.
Char *A = {‘a’,’b’,’c’};
Char *B = {‘x’,’y’,’z’};
B = A;
Now B is pointing is at same location where A pointer is pointing.Both A and B in this case sharing same data. if change is made both will get altered value of data.Advantage is that coping process is very fast and is independent of size of array.

while in deep copy data is also copied. This process is slow but Both A and B have their own copies and changes made to any copy, other will copy will not be affected.  


What is purpose of delegates?
Answer:- 

A delegate allows one object to send messages to another object when an event happens. For example, if you're downloading data from a web site asynchronously using the NSURLConnection class. NSURLConnection has three comment delegates:

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
-(void)connection:(NSURLConnection *)connection didReceiveResponse
- (NSURLResponse *)response  

No comments:

Post a Comment