What are blocks and how are they used?


Blocks are a way of defining a single task or unit of behavior without having to write an entire Objective-C class. Under the covers Blocks are still Objective C objects. They are a language level feature that allow programming techniques like lambdas and closures to be supported in Objective-C. Creating a block is done using the ^ { } syntax:

myBlock = ^{
   NSLog(@"This is a block");

It can be invoked like so:


It is essentially a function pointer which also has a signature that can be used to enforce type safety at compile and runtime. For example you can pass a block with a specific signature to a method like so:

- (void)callMyBlock:(void (^)(void))callbackBlock;

If you wanted the block to be given some data you can change the signature to include them:

- (void)callMyBlock:(void (^)(double, double))block 
    block(3.0, 2.0);

What’s the difference between not-running, inactive, active, background and suspended execution states?

  • Not running: The app has not been launched or was running but was terminated by the system.
  • Inactive: The app is running in the foreground but is currently not receiving events. (It may be executing other code though.) An app usually stays in this state only briefly as it transitions to a different state.
  • Active: The app is running in the foreground and is receiving events. This is the normal mode for foreground apps.
  • Background: The app is in the background and executing code. Most apps enter this state briefly on their way to being suspended. However, an app that requests extra execution time may remain in this state for a period of time. In addition, an app being launched directly into the background enters this state instead of the inactive state.
  • Suspended: The app is in the background but is not executing code. The system moves apps to this state automatically and does not notify them before doing so. While suspended, an app remains in memory but does not execute any code. When a low-memory condition occurs, the system may purge suspended apps without notice to make more space for the foreground app.

View LifeCycle

First Time Call :

1. viewDidLoad
2. viewWillAppear
3. viewDidAppear

If Push To Another(second) ViewController :

1. FirstVC :- viewWillDisappear
2. SecondVC :- viewDidLoad
3. SecondVC :- viewWillAppear
4. FirstVC :- viewDidDisappear
5. SecondVC :- viewDidAppear

If POP To Back(First) ViewController :

1. SecondVC :- viewWillDisappear
2. FirstVC :- viewWillAppear
3. SecondVC :- viewDidDisappear
4. FirstVC :- viewDidAppear

Application Life Cycle

First Time Call :

1. application didFinishLaunchingWithOptions
2. applicationDidBecomeActive

In Background :

1. applicationWillResignActive
2. applicationDidEnterBackground

In ForeGround :

1. applicationWillEnterForeground
2. applicationDidBecomeActive

Describe Property

Before you know about the attributes of @property, you should know what is the use of @property.

  • @property offers a way to define the information that a class is intended to encapsulate. If you declare an object/variable using @property, then that object/variable will be accessible to other classes importing its class.
  • If you declare an object using @property in the header file, then you have to synthesize it using @synthesize in the implementation file. This makes the object KVC compliant. By default, compiler will synthesize accessor methods for this object.
  • accessor methods are : setter and getter.

Example: “.h” File
@interface XYZClass : NSObject
@property (nonatomic, retain) NSString *name;

Example: “.m” File
@implementation XYZClass
@synthesize name;

Now the compiler will synthesize accessor methods for name.
XYZClass *obj=[[XYZClass alloc]init];
NSString *name1=[obj name]; // get 'name'
[obj setName:@"liza"]; // first letter of 'name' becomes capital in setter method

List Of Property
1. atomic //default
2. nonatomic
3. retain
4. copy
5. readonly
6. readwrite
7. assign
8. strong
9. getter=method
10. setter=method
11. unsafe_unretained

1. atomic :
atomic is the default behavior. If an object is declared as atomic then it becomes thread-safe. Thread-safe means, at a time only one thread of a particular instance of that class can have the control over that object.

If the thread is performing getter method then other thread cannot perform setter method on that object. It is slow

@property NSString *name; //by default atomic`

@property (atomic)NSString *name; // explicitly declared atomic`

2. nonatomic :
nonatomic is not thread-safe. You can use the nonatomic property attribute to specify that synthesized accessors simply set or return a value directly, with no guarantees about what happens if that same value is accessed simultaneously from different threads

For this reason, it’s faster to access a nonatomic property than an atomic one.

@property (nonatomic)NSString *name; 

3. retain :
retain is required when the attribute is a pointer to an object.

The setter method will increase retain count of the object, so that it will occupy memory in autorelease pool.

@property (retain)NSString *name;

4. copy :
copy If you use copy, you can’t use retain. Using copy instance of the class will contain its own copy.

Even if a mutable string is set and subsequently changed, the instance captures whatever value it has at the time it is set. No setter and getter methods will be synthesized.

@property (copy) NSString *name;
NSMutableString *nameString = [NSMutableString stringWithString:@"Liza"];    
xyzObj.name = nameString;    
[nameString appendString:@"Pizza"]; 

name will remain unaffected.

5. readonly :
readonly If you don’t want to allow the property to be changed via setter method, you can declare the property readonly.

Compiler will generate a getter, but not a setter.

@property (readonly) NSString *name;

6. readwrite :
readwrite is the default behavior. You don’t need to specify readwrite attribute explicitly.

It is opposite of readonly.

@property (readwrite) NSString *name;

7. assign :
assign will generate a setter which assigns the value to the instance variable directly, rather than copying or retaining it. This is best for primitive types like NSInteger and CGFloat, or objects you don’t directly own, such as delegates.

Keep in mind retain and assign are basically interchangeable when garbage collection is enabled.

@property (assign) NSInteger year;

8. strong :
strong is a replacement for retain.

It comes with ARC.

@property (nonatomic, strong) AVPlayer *player; 

9. getter=method :
getter=method If you want to use a different name for a getter method, it’s possible to specify a custom name by adding attributes to the property.

In the case of Boolean properties (properties that have a YES or NO value), it’s customary for the getter method to start with the word “is”

@property (getter=isFinished) BOOL finished;

10. setter=method :
setter=method If you want to use a different name for a setter method, it’s possible to specify a custom name by adding attributes to the property.

The method should end with a colon.

@property(setter = boolBool:) BOOL finished;

10. unsafe_unretained :
unsafe_unretained There are a few classes in Cocoa and Cocoa Touch that don’t yet support weak references, which means you can’t declare a weak property or weak local variable to keep track of them. These classes include NSTextView, NSFont and NSColorSpace,etc. If you need to use a weak reference to one of these classes, you must use an unsafe reference.

An unsafe reference is similar to a weak reference in that it doesn’t keep its related object alive, but it won’t be set to nil if the destination object is deallocated.

@property (unsafe_unretained) NSObject *unsafeProperty;

If you need to specify multiple attributes, simply include them as a comma-separated list, like this:

@property (readonly, getter=isFinished) BOOL finished;