Tuesday, November 5, 2013

What is autorelease pool?

Autorelease pool is an object (instance of NSAutorelease class) which will be created at the starting of thread run loop and released at the end of run loop.

When you call autorelease on any object that object will be added to autorelease pool and finally when pool is getting released all the objects in the pool will be released. The main purpose of autorelease is to release the object some time later(at end of run loop) but not immediately.


Monday, November 4, 2013

What is Selector?

A selector is the unique identifier that replaces the method name when the source code is compiled.Compiled selectors are of type SEL.To get selector for any method use @selector(methodName).

What is Singleton class?

A Singleton class returns the same instance no matter how many times an application requests it. 
Example: UIApplication, UIAccelerometer, NSFileManager etc

For making our custom class(say Car) as singleton we need to create a class method as below:

+(id)sharedCar{
 static id singleInstance=nil;
if(singleInstance==nil){
singleInstance =[[Car alloc]init];
}
 return singleInstance;
}

What is Protocol?

Protocols are of two types
1.Formal protocol
A formal protocol declares a list of optional and required methods that other classes are expected to implement.

Syntax:
@protocol protocolName
   //required method declarations
   @optional
   //optional method declarations
   @required
   //other required method declarations
@end
  2.Informal Protocol
An informal protocol is a category(with out implementation) on NSObject, which implicitly makes almost all objects adopters of the protocol.Implementation of the methods in an informal protocol are optional. 

What is Category?

Category is an objective-c feature which allows to add additional methods to a class for which we don't have source code.We can add additional methods to classes like NSString,NSArray etc.

Any methods that you declare in a category will be available to all instances of the original class, as well as any subclasses of the original class. At runtime, there’s no difference between a method added by a category and one that is implemented by the original class.