IOS開發(57)之構建 Block Objects

來源:互聯網
上載者:User

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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.