1 Preface
This article describes how to call Block Object by using functions and how to call Block Object by using Block Object.
2. code example
TestDemo. h
[Plain]
# Import <Foundation/Foundation. h>
@ Interface TestDemo: NSObject
-(Void) callSimpleBlock;
-(Void) callTrimBlock;
@ End
# Import <Foundation/Foundation. h>
@ Interface TestDemo: NSObject
-(Void) callSimpleBlock;
-(Void) callTrimBlock;
@ End
TestDemo. m
[Plain]
# Import "TestDemo. h"
@ Implementation TestDemo
/************** Call the Block Object Start ***************/
Void (^ simpleBlock) (NSString *) = ^ (NSString * paramString ){
/* Implement the block object here and use the paramString parameter */
NSLog (@ "% @", paramString );
};
-(Void) callSimpleBlock {
SimpleBlock (@ "Archy ");
}
/************** Call the Block Object End ***************/
/************** Block Object call Block Object Start ***************/
NSString * (^ trimString) (NSString *) = ^ (NSString * inputString ){
NSString * result = [inputString stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet];
Return result;
};
NSString * (^ trimWithOtherBlock) (NSString *) = ^ (NSString * inputString ){
Return trimString (inputString );
};
-(Void) callTrimBlock {
NSString * trimmedString = trimWithOtherBlock (@ "Archy ");
NSLog (@ "Trimmed string = % @", trimmedString );
}
/************** Block Object call Block Object Start ***************/
@ End
# Import "TestDemo. h"
@ Implementation TestDemo
/************** Call the Block Object Start ***************/
Void (^ simpleBlock) (NSString *) = ^ (NSString * paramString ){
/* Implement the block object here and use the paramString parameter */
NSLog (@ "% @", paramString );
};
-(Void) callSimpleBlock {
SimpleBlock (@ "Archy ");
}
/************** Call the Block Object End ***************/
/************** Block Object call Block Object Start ***************/
NSString * (^ trimString) (NSString *) = ^ (NSString * inputString ){
NSString * result = [inputString stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet];
Return result;
};
NSString * (^ trimWithOtherBlock) (NSString *) = ^ (NSString * inputString ){
Return trimString (inputString );
};
-(Void) callTrimBlock {
NSString * trimmedString = trimWithOtherBlock (@ "Archy ");
NSLog (@ "Trimmed string = % @", trimmedString );
}
/************** Block Object call Block Object Start ***************/
@ End
Main. m
[Plain] view plaincopyprint? Int main (int argc, const char * argv [])
{
@ Autoreleasepool {
TestDemo * test = [[TestDemo alloc] init];
// [Test callSimpleBlock];
[Test callTrimBlock];
}
Return 0;
}
Int main (int argc, const char * argv [])
{
@ Autoreleasepool {
TestDemo * test = [[TestDemo alloc] init];
// [Test callSimpleBlock];
[Test callTrimBlock];
}
Return 0;
}
Running result
06:53:50. 893 CallBlockObjectTest [591: 303] Trimmed string = Archy