1 Preface
Objects of Block Objects can be inline or encoded as independent code blocks. Today we will introduce its usage.
2. code example
Today, we do not need to develop the IOS framework. We will use simple programs for development. Next we will introduce how to use Xcode to build a simple program.
2.1 create a project
2.2 select "Type" as "Foundation ".
2.3 create a new class and add the method to be called
TestBlockObject. h
[Plain]
# Import <Foundation/Foundation. h>
@ Interface TestBlockObject: NSObject
// Normal method
-(NSInteger) subtract :( NSInteger) paramValue from :( NSInteger) paramFrom;
// Call Block Object
-(Void) callIntToString;
-(Void) doTheConversion;
-(Void) doTheConversionExt;
-(Void) doTheConversionExt2;
@ End
# Import <Foundation/Foundation. h>
@ Interface TestBlockObject: NSObject
// Normal method
-(NSInteger) subtract :( NSInteger) paramValue from :( NSInteger) paramFrom;
// Call Block Object
-(Void) callIntToString;
-(Void) doTheConversion;
-(Void) doTheConversionExt;
-(Void) doTheConversionExt2;
@ End
TestBlockObject. m
[Plain]
# Import "TestBlockObject. h"
@ Implementation TestBlockObject
-(NSInteger) subtract :( NSInteger) paramValue from :( NSInteger) paramFrom {
Return paramFrom-paramValue;
}
// Block Object
NSString * (^ intToString) (NSUInteger) = ^ (NSUInteger paramInteger ){
NSString * result = [NSString stringWithFormat: @ "% lu", (unsigned long) paramInteger];
Return result;
};
-(Void) callIntToString {
NSString * string = intToString (10 );
NSLog (@ "string = % @", string );
}
// Typedef indicates the intToStringBlock Object signature. This signature will tell the compiler what parameters will be accepted for our Block Object:
// This typedef tells the compiler that Block Objects accepts an integer parameter and returns a string that is displayed by the IntToString Converter name.
Typedef NSString * (^ IntToStringConverter) (NSUInteger paramInteger );
-(NSString *) convertIntToString :( NSUInteger) paramInteger
UsingBlockObject :( IntToStringConverter) paramBlockObject
{
Return paramBlockObject (paramInteger );
}
-(Void) doTheConversion {
NSString * result = [self convertIntToString: 123 usingBlockObject: intToString];
NSLog (@ "result = % @", result );
}
-(Void) doTheConversionExt {
IntToStringConverter inlineConverter = ^ (NSUInteger paramInteger ){
NSString * result = [NSString stringWithFormat: @ "% lu", (unsigned long) paramInteger];
Return result ;};
NSString * result = [self convertIntToString: 123 usingBlockObject: inlineConverter];
NSLog (@ "result = % @", result );
}
// Inline
-(Void) doTheConversionExt2 {
NSString * result =
[Self convertIntToString: 123 usingBlockObject: ^ NSString * (NSUInteger paramInteger)
{
NSString * result = [NSString stringWithFormat: @ "% lu", (unsigned long) paramInteger];
Return result;
}];
NSLog (@ "result = % @", result );
}
# Import "TestBlockObject. h"
@ Implementation TestBlockObject
-(NSInteger) subtract :( NSInteger) paramValue from :( NSInteger) paramFrom {
Return paramFrom-paramValue;
}
// Block Object
NSString * (^ intToString) (NSUInteger) = ^ (NSUInteger paramInteger ){
NSString * result = [NSString stringWithFormat: @ "% lu", (unsigned long) paramInteger];
Return result;
};
-(Void) callIntToString {
NSString * string = intToString (10 );
NSLog (@ "string = % @", string );
}
// Typedef indicates the intToStringBlock Object signature. This signature will tell the compiler what parameters will be accepted for our Block Object:
// This typedef tells the compiler that Block Objects accepts an integer parameter and returns a string that is displayed by the IntToString Converter name.
Typedef NSString * (^ IntToStringConverter) (NSUInteger paramInteger );
-(NSString *) convertIntToString :( NSUInteger) paramInteger
UsingBlockObject :( IntToStringConverter) paramBlockObject
{
Return paramBlockObject (paramInteger );
}
-(Void) doTheConversion {
NSString * result = [self convertIntToString: 123 usingBlockObject: intToString];
NSLog (@ "result = % @", result );
}
-(Void) doTheConversionExt {
IntToStringConverter inlineConverter = ^ (NSUInteger paramInteger ){
NSString * result = [NSString stringWithFormat: @ "% lu", (unsigned long) paramInteger];
Return result ;};
NSString * result = [self convertIntToString: 123 usingBlockObject: inlineConverter];
NSLog (@ "result = % @", result );
}
// Inline
-(Void) doTheConversionExt2 {
NSString * result =
[Self convertIntToString: 123 usingBlockObject: ^ NSString * (NSUInteger paramInteger)
{
NSString * result = [NSString stringWithFormat: @ "% lu", (unsigned long) paramInteger];
Return result;
}];
NSLog (@ "result = % @", result );
}
Main. m
[Plain]
Int main (int argc, const char * argv [])
{
@ Autoreleasepool {
TestBlockObject * test = [[TestBlockObject alloc] init];
NSInteger I = [test subtract: 10 from: 20];
NSLog (@ "Simple method [test subtract: 10 from: 20] result is ===> % ld", (long) I );
NSLog (@ "callingBlockObject ");
[Test callIntToString];
NSLog (@ "usingBlockObject ");
[Test doTheConversion];
NSLog (@ "doTheConversionExt ===> ");
[Test doTheConversionExt];
NSLog (@ "doTheConversionExt2 ===> ");
[Test doTheConversionExt2];
[Test release];
// Block Object
NSInteger (^ subtract) (NSInteger, NSInteger) = ^ (NSInteger paramValue, NSInteger paramFrom) {return paramFrom-paramValue;
};
NSLog (@ "Simple Block Object result is % lu", subtract (10, 20 ));
}
Return 0;
}
Int main (int argc, const char * argv [])
{
@ Autoreleasepool {
TestBlockObject * test = [[TestBlockObject alloc] init];
NSInteger I = [test subtract: 10 from: 20];
NSLog (@ "Simple method [test subtract: 10 from: 20] result is ===> % ld", (long) I );
NSLog (@ "callingBlockObject ");
[Test callIntToString];
NSLog (@ "usingBlockObject ");
[Test doTheConversion];
NSLog (@ "doTheConversionExt ===> ");
[Test doTheConversionExt];
NSLog (@ "doTheConversionExt2 ===> ");
[Test doTheConversionExt2];
[Test release];
// Block Object
NSInteger (^ subtract) (NSInteger, NSInteger) = ^ (NSInteger paramValue, NSInteger paramFrom) {return paramFrom-paramValue;
};
NSLog (@ "Simple Block Object result is % lu", subtract (10, 20 ));
}
Return 0;
}
Console result after running
17:35:45. 335 TestBlockObject [1986: 303] Simple method [test subtract: 10 from: 20] result is ===> 10
17:35:45. 337 TestBlockObject [1986: 303] callingBlockObject
17:35:45. 338 TestBlockObject [1986: 303] string = 10
17:35:45. 339 TestBlockObject [1986: 303] usingBlockObject
17:35:45. 339 TestBlockObject [1986: 303] result = 123
17:35:45. 340 TestBlockObject [1986: 303] doTheConversionExt ===>
17:35:45. 340 TestBlockObject [1986: 303] result = 123
17:35:45. 341 TestBlockObject [1986: 303] doTheConversionExt2 ===>
17:35:45. 341 TestBlockObject [1986: 303] result = 123
17:35:45. 342 TestBlockObject [1986: 303] Simple Block Object result is 10