1 前言
Block Objects 的對象可以內聯或編碼為獨立的代碼塊。 今天我們就來介紹一下其用法。
2 代碼實例
今天我們不用IOS框架來開發,直接用簡單的程序來開發,下面來介紹一下如何用Xcode建立一個簡單的程序
2.1 新建項目
2.2 選擇Type為Foundation
2.3 新建一個類,在裡面添加需要調用的相應方法
TestBlockObject.h
[plain]
#import <Foundation/Foundation.h>
@interface TestBlockObject : NSObject
//正常方法
- (NSInteger) subtract:(NSInteger)paramValue from:(NSInteger)paramFrom;
//調用Block Object
- (void) callIntToString;
- (void) doTheConversion;
- (void) doTheConversionExt;
- (void) doTheConversionExt2;
@end
#import <Foundation/Foundation.h>
@interface TestBlockObject : NSObject
//正常方法
- (NSInteger) subtract:(NSInteger)paramValue from:(NSInteger)paramFrom;
//調用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 這個 intToStringBlock Object 的簽名, 這個簽名會告訴 編譯器我們的 Block Object 會接受什麼參數:
//這個 typedef 告訴編譯器 Block Objects 接受一個整數參數並且返回一個被 IntToString Converter 命名的標 示符來展現的字符串。
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);
}
//內聯
- (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 這個 intToStringBlock Object 的簽名, 這個簽名會告訴 編譯器我們的 Block Object 會接受什麼參數:
//這個 typedef 告訴編譯器 Block Objects 接受一個整數參數並且返回一個被 IntToString Converter 命名的標 示符來展現的字符串。
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);
}
//內聯
- (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;
}
運行後控制台結果
2013-05-09 17:35:45.335 TestBlockObject[1986:303] Simple method [test subtract:10 from:20] result is====>10
2013-05-09 17:35:45.337 TestBlockObject[1986:303] callingBlockObject
2013-05-09 17:35:45.338 TestBlockObject[1986:303] string = 10
2013-05-09 17:35:45.339 TestBlockObject[1986:303] usingBlockObject
2013-05-09 17:35:45.339 TestBlockObject[1986:303] result = 123
2013-05-09 17:35:45.340 TestBlockObject[1986:303] doTheConversionExt=====>
2013-05-09 17:35:45.340 TestBlockObject[1986:303] result = 123
2013-05-09 17:35:45.341 TestBlockObject[1986:303] doTheConversionExt2=====>
2013-05-09 17:35:45.341 TestBlockObject[1986:303] result = 123
2013-05-09 17:35:45.342 TestBlockObject[1986:303] Simple Block Object result is 10