IOS开发(57)之构建 Block Objects

来源:互联网 发布:淘宝商城旺旺 编辑:程序博客网 时间:2024/06/14 05:43

1 前言

Block Objects 的对象可以内联或编码为独立的代码块。 今天我们就来介绍一下其用法。

2 代码实例

今天我们不用IOS框架来开发,直接用简单的程序来开发,下面来介绍一下如何用Xcode建立一个简单的程序

2.1 新建项目


2.2 选择Type为Foundation


2.3 新建一个类,在里面添加需要调用的相应方法


TestBlockObject.h

#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

#import "TestBlockObject.h"@implementation TestBlockObject- (NSInteger) subtract:(NSInteger)paramValue from:(NSInteger)paramFrom{    return paramFrom - paramValue;}//Block ObjectNSString* (^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

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

3 结语

以上就是所有内容,希望对大家有所帮助。

Demo下载地址:http://download.csdn.net/detail/u010013695/5349756

原创粉丝点击