- Command-Line Arguments
- Objective-C - Error Handling
- Objective-C - Log Handling
- Objective-C - Type Casting
- Objective-C - Typedef
- Objective-C - Preprocessors
- Objective-C - Structures
- Objective-C - Strings
- Objective-C - Pointers
- Objective-C - Arrays
- Objective-C - Numbers
- Objective-C - Blocks
- Objective-C - Functions
- Objective-C - Decision Making
- Objective-C - Loops
- Objective-C - Operators
- Objective-C - Constants
- Objective-C - Variables
- Objective-C - Data Types
- Objective-C - Basic Syntax
- Objective-C - Program Structure
- Objective-C - Environment Setup
- Objective-C - Overview
- Objective-C - Home
Advanced Objective-C
- Obj-C - Memory Management
- Objective-C - Fast Enumeration
- Obj-C - Foundation Framework
- Objective-C - Composite Objects
- Objective-C - Dynamic Binding
- Objective-C - Protocols
- Objective-C - Extensions
- Objective-C - Posing
- Objective-C - Categories
- Objective-C - Data Encapsulation
- Objective-C - Polymorphism
- Objective-C - Inheritance
- Objective-C - Classes & Objects
Objective-C Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Objective-C Blocks
An Objective-C class defines an object that combines data with related behavior. Sometimes, it makes sense just to represent a single task or unit of behavior, rather than a collection of methods.
Blocks are a language-level feature added to C, Objective-C and C++ which allow you to create distinct segments of code that can be passed around to methods or functions as if they were values. Blocks are Objective-C objects which means they can be added to collections pke NSArray or NSDictionary. They also have the abipty to capture values from the enclosing scope, making them similar to closures or lambdas in other programming languages
Simple Block declaration syntax
returntype (^blockName)(argumentType);
Simple block implementation
returntype (^blockName)(argumentType)= ^{ };
Here is a simple example
void (^simpleBlock)(void) = ^{ NSLog(@"This is a block"); };
We can invoke the block using
simpleBlock();
Blocks Take Arguments and Return Values
Blocks can also take arguments and return values just pke methods and functions.
Here is a simple example to implement and invoke a block with arguments and return values.
double (^multiplyTwoValues)(double, double) = ^(double firstValue, double secondValue) { return firstValue * secondValue; }; double result = multiplyTwoValues(2,4); NSLog(@"The result is %f", result);
Blocks Using Type Definitions
Here is a simple example using typedef in block. Please note this sample doesn t work on the onpne compiler for now. Use XCode to run the same.
#import <Foundation/Foundation.h> typedef void (^CompletionBlock)(); @interface SampleClass:NSObject - (void)performActionWithCompletion:(CompletionBlock)completionBlock; @end @implementation SampleClass - (void)performActionWithCompletion:(CompletionBlock)completionBlock { NSLog(@"Action Performed"); completionBlock(); } @end int main() { /* my first program in Objective-C */ SampleClass *sampleClass = [[SampleClass alloc]init]; [sampleClass performActionWithCompletion:^{ NSLog(@"Completion is called to intimate action is performed."); }]; return 0; }
Let us compile and execute it, it will produce the following result −
2013-09-10 08:13:57.155 demo[284:303] Action Performed 2013-09-10 08:13:57.157 demo[284:303] Completion is called to intimate action is performed.
Blocks are used more in iOS apppcations and Mac OS X. So its more important to understand the usage of blocks.
Advertisements