2013年7月6日 星期六

Block的使用3-typedef的使用

文章攢寫時間︰2013/07/07 15:02

前兩篇我們提到了Block的使用,
這篇來談談要怎麼簡易的初始化一個Block。

tyepdef float (^MyBlockType)(float,float);

typedef後面的東西,
就是原本在宣告Block的等於的左半部。

使用typeDef後︰

MyBlockType myFirstBlock = // ... ;

這樣子用起來是不是輕鬆多了?

相關文章︰

1. Block的使用1
2. Block的使用2-注意事項

Block的使用2-注意事項

文章攢寫時間︰2013/07/07 14:33

Block使用注意事項

前一面提到Block的使用,
這裡提到一點蠻特別、而且也可能在日後造成程式BUG的地方

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    ViewController *myView = [ [ViewController alloc] init];
    myView.delegate = self;
    [myView willCallDelegatePrint];
    [myView willCallDelegateLog:@"Hello myDelegate"];
        NSLog(@"sub viewDidLoad");
    
    int outsideValue = 123;
    
    int (^myBlock)(int, NSString*) = ^(int num, NSString* str)
    {
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"I am Title" message:str delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Btn1" , nil];
        [alert show];
        
        //Block對外面的值只能是ReadOnly
        return outsideValue;
    };
    
    outsideValue = 456;
    NSLog(@"get int: %d",myBlock(567,@"白七")); 
    
}
 

如果以程式的先打先執行概念來看,
因為myBlock在outsideValue=456之後才呼叫,
NSLog應該會印岀outsideValue為456,
但是,卻印岀值為123

以下是執行的結果

Block只會使用變數第1次宣告時的值。

如果希望印出來是456,
那麼outSideValue在宣告時,
前面要加上__block關鍵字。
以下是修改後的Code︰

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    ViewController *myView = [ [ViewController alloc] init];
    myView.delegate = self;
    [myView willCallDelegatePrint];
    [myView willCallDelegateLog:@"Hello myDelegate"];
        NSLog(@"sub viewDidLoad");
    
    __block int outsideValue = 123;
    
    int (^myBlock)(int, NSString*) = ^(int num, NSString* str)
    {
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"I am Title" message:str delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Btn1" , nil];
        [alert show];
        
        //Block對外面的值只能是ReadOnly
        return outsideValue;
    };
    
    outsideValue = 456;
    NSLog(@"get int: %d",myBlock(567,@"白七")); 
    
}
 
結果如下

相關文章︰

1. Block的使用1
2. Block的使用3-typedef的使用

Block的使用1

文章攢寫時間︰20130707 13:43

Block

  • 用起來像普通的Method
  • 可以在Method裡又寫一段Code,並存取在Block之外的所有變數(但是只能Read-Only)。
  • 用起來的概念像callback


底下在viewDidLoad這個函式中,
宣告了myBlock這個Block並在裡面呼叫了UIAlertView。

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    ViewController *myView = [ [ViewController alloc] init];
    myView.delegate = self;
    [myView willCallDelegatePrint];
    [myView willCallDelegateLog:@"Hello myDelegate"];
        NSLog(@"sub viewDidLoad");
    
    int outsideValue = 123;
    int (^myBlock)(int, NSString*) = ^(int num, NSString* str)
    {
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"I am Title" message:str delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Btn1" , nil];
        [alert show];
        
        //Block對外面的值只能是ReadOnly
        return num+outsideValue;
    };
    
    NSLog(@"get added int: %d",myBlock(567,@"白七")); 
}
 
結果如下

相關文章︰

1. Block的使用2-注意事項
2. Block的使用3-typedef的使用

Delegation(委派)的使用

文章攢寫時間︰20130707 10:26

Delegation是一種design pattern,方便在各個物件之間傳遞參數以及互相支援。
例如UITableView如何知道目前這張Table需要多少Session或是多少個Cell?

使用protocal關鍵字
@protocol <protocol name>//在A.h檔中宣告
@required //在A.h檔中宣告此函式"一定"要被實作
@optional //在A.h檔中宣告此函式"不一定"要被實作
 

Category類目的使用

文章攢寫時間︰20130707 09:30

類目(Category)
類目是設計給一群設計師共同編輯使用的特殊Object-C用法。

宣告一個@interface,
但是不要繼承NSObject,
並在後面宣告一個類目的名稱。
假設這是原先最基本實作NSObject的方法︰
@interface Fraction : NSObject
 
現在我們會改成這樣︰
@interface Fraction (Mathxxx)