Saturday, January 4, 2014

What is the difference between a deep copy and a shallow copy?

There are two kinds of object copying:  shallow copies and deep copies.

The normal copy is a shallow copy that produces a new collection that shares ownership of the objects with the original.

Deep copies create new objects from the originals and add those to the new collection.

 

What is the basic difference between coredata and sqlite?

SQLite is a relational database. But CoreData is an objective-c framework  which acts as a layer between the database and the UI.

What is the maximum size of SQLite Data database on iOS?

The max size of your SQLite database is only limited by free disk space.

What happens when you invoke a method on a nil pointer?

In objective-c calling a method on nil is valid and will be like no operation.

When dealing with property declarations, what is the difference between atomic and non-atomic?

atomic -Atomic means only one thread access the variable.Atomic is thread safe,but it is slow in performance.atomic is default behavior.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. atomic it is not actually a keyword.

Ex: @property (retain) NSString *name; 
 
nonatomic -Nonatomic means multiple thread access the variable. Nonatomic is thread unsafe,but it is fast in performance.Nonatomic is NOT default behavior,we need to add nonatomic keyword in property attribute.It may result in unexpected behavior, when two different threads access the same variable at the same time.

Ex: @property (nonatomic,retain) NSString *name; 

Explain:
Suppose there is an atomic string property called "name", and if you call [self setName:@"A"] from thread A, call [self setName:@"B"] from thread B, and call [self name] from thread C, then all operation on different thread will be performed serially which means if one thread is executing setter or getter, then other threads will wait. This makes property "name" read/write safe but if another thread D calls [name release] simultaneously then this operation might produce a crash because there is no setter/getter call involved here. Which means an object is read/write safe (ATOMIC) but not thread safe as another threads can simultaneously send any type of messages to the object. Developer should ensure thread safety for such objects.

If the property "name" was nonatomic, then all threads in above example - A,B, C and D will execute simultaneously producing any unpredictable result. In case of atomic, Either one of A, B or C will execute first but D can still execute in parallel.

List the five iOS app states?

1.not running
2.inactive
3.active
4.background
5.suspended


What happens when the following code executes? Ball *ball = [[[[Ball alloc] init] autorelease] autorelease];

The application will crash.

Reason:

when alloc is called on Ball, object will be created and its retain count will be set to one.
Because of autorlease calls the ball object will be added to autorelease pool twice.
When pool is getting released release method will be called on the ball object twice.
When first time release method is called ball objects reatin count will become zero and object will be destroyed.Now when second time release method is called on that destroyed object  application will crash.

What is the difference between frames and bounds?

The bounds of a view is the rectangle, expressed as a location (x,y) and size (width,height) relative to its own coordinate system (0,0).

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.

So, imagine a view that has a size of 100x100 (width x height) positioned at 25,25 (x,y) of its superview. The following code prints out this view's bounds and frame:

// This method is in the view controller of the superview
- (void)viewDidLoad {
    [super viewDidLoad];

    NSLog(@"bounds.origin.x: %f", label.bounds.origin.x);
    NSLog(@"bounds.origin.y: %f", label.bounds.origin.y);
    NSLog(@"bounds.size.width: %f", label.bounds.size.width);
    NSLog(@"bounds.size.height: %f", label.bounds.size.height);

    NSLog(@"frame.origin.x: %f", label.frame.origin.x);
    NSLog(@"frame.origin.y: %f", label.frame.origin.y);
    NSLog(@"frame.size.width: %f", label.frame.size.width);
    NSLog(@"frame.size.height: %f", label.frame.size.height);
}
 
And the output of this code is:

bounds.origin.x: 0 
bounds.origin.y: 0 
bounds.size.width: 100 
bounds.size.height: 100 
frame.origin.x: 25 
frame.origin.y: 25 
frame.size.width: 100 
frame.size.height: 100
 
 So, we can see that in both cases, the width and the height of the view is the same regardless of whether we are looking at the bounds or frame. What is different is the x,y positioning of the view. In the case of the bounds, the x and y coordinates are at 0,0 as these coordinates are relative to the view itself. However, the frame x and y coordinates are relative to the position of the view within the parent view (which earlier we said was at 25,25).

what is difference between retain and copy?

when reatin is called on any object its reatinCount will be incremented by 1.

when copy is called on any object  new copy (or clone) will be created with retain count set to 1.

What is plist (property list)?


Property lists are a convenient way to access and store small amounts of data. You cannot store custom data objects in a plist, only standard types(NSArray,NSDictionary,NSString,NSDate,NSData,NSNumber) can be stored. By default plists  store data in XML format though they can also be stored in binary format. Property lists are very user friendly because there are methods already in place to store a Dictionary or Array of objects as a plist xml document and to then other methods to read a plist xml document and convert it back into an array or dictionary etc

For reading plist we can use below methods:
+arrayWithContentsOfFile:
+dictionaryWithContentsOfFile

For Writing into plist we we can use below method:
-writeToFile:atomically: