標籤:
1、block2、protocol
一、block
block 程式碼片段
標識是 ^
block 和函數很像
1、可以儲存代碼
2、有傳回值
3、有形參
格式
傳回值 (block名)(形參列表) = ^(形參列表) {程式碼片段};
使用方法:block名(形參);
block 總結:
1、定義
// 沒有傳回值 ,沒有形參可以省略後面的小括弧
void (^myblock)() = ^
{
NSLog(@"****************");
};
myblock(); // 使用
// 帶參數和傳回值的block
int (^SumBlock)(int,int) = ^(int a,int b)
{
return a + b;
};
2、block訪問外部變數
block可以訪問外部變數
預設情況下,block 不能修改外部的局部變數
給局部變數加上__block 關鍵字就可以在block內部修改
int a = 10;
__block int b = 20;
void (^block) = ^
{
b = 40; // 現在修改沒有問題
}
3、利用typdef定義block類型
// 使用typdef簡化代碼
typedef int (^MyBlock) (int, int);
// 使用也很簡單
MyBlock block1 = ^(int a, int b)
{
return a * b;
};
程式碼範例:
1 // 2 // main.m 3 // 05_block 4 // 5 // Created by Christian on 15/4/19. 6 // Copyright (c) 2015年 Christian. All rights reserved. 7 // 8 9 #import <Foundation/Foundation.h>10 11 // 使用typdef簡化代碼12 typedef int (^MyBlock) (int, int);13 14 void test()15 {16 NSLog(@"****************");17 }18 19 int Sum(int a,int b)20 {21 return a + b;22 }23 24 int main()25 { 26 // 沒有傳回值 ,沒有形參可以省略後面的小括弧27 void (^myblock)() = ^28 {29 NSLog(@"****************");30 NSLog(@"****************");31 };32 myblock(); // 使用33 34 // 帶參數和傳回值的block35 int (^SumBlock)(int,int) = ^(int a,int b)36 {37 return a + b;38 };39 40 NSLog(@"%d",SumBlock(2,4));41 42 // 使用typdef簡化代碼43 MyBlock block1 = ^(int a, int b)44 {45 return a * b;46 };47 NSLog(@"%d",block1(5,4));48 49 50 // 函數指標51 int (*p)(int,int) = Sum; // block 和函數指標類似52 NSLog(@"sum is %d",p(5,7));53 54 return 0;55 }
二、 protocol
// 定義了一個名字叫@MyProtocol的協議
@protocol MyProtocol
1 #import <Foundation/Foundation.h> 2 3 // 定義了一個名字叫@MyProtocol的協議 4 @protocol MyProtocol 5 6 @required // 要求實現方法,不實現會出現警告,預設是@required 7 - (void)test; 8 @optional // 可選,可實現也可不實現,沒有警告 9 - (void)test2;10 11 12 @end
遵守協議使用 <>
1 #import <Foundation/Foundation.h>2 #import "MyProtocol.h"3 4 // 只要一個類遵守了某個協議,就能使用這個協議裡聲明的方法。5 // <> 用來遵守協議6 @interface Person : NSObject <MyProtocol>7 8 @end
類可以遵守多個協議
1 #import <Foundation/Foundation.h>2 3 @protocol MyProtocol2 4 5 @required6 - (void)haha2;7 @optional8 - (void)haha3;9 @end
1 @interface Person : NSObject <MyProtocol, MyProtocol2> // 遵守多個協議2 3 @end
基協議
1、NSObject是一個基類,最根本最基本的類,任何其他類最終都要繼承它
2、其實還有一個協議,名字也叫NSObject,它是一個基協議,最根本最基本的協議
3、NSObject協議中聲明很多最基本的方法,比如description、retain、release等
4、建議每個新的協議都要遵守NSObject協議
1 #import <Foundation/Foundation.h>2 3 @protocol MyProtocol2 <NSObject> // 遵守基協議4 5 @required6 - (void)haha2;7 @optional8 - (void)haha3;9 @end
產生指向特定的協議
1 // 定義一個只能指向遵守協議MyPtotocol的指標2 NSObject<MyProtocol> *obj = [[Person alloc] init]; // Person遵守了MyProtocol,所以可以賦值。3 4 // id 不需要寫指標5 id<MyProtocol> obj2 = [[Person alloc] init];6
協議也可以在.h標頭檔中只聲明,在.m檔案中包含標頭檔
和@class Dog;原理一樣。
聲明
@protocol MyProtocol;
@protocol MyProtocol, MyProtocol2;
協議總結:
1、定義
@protocol MyProtocol <NSObject>
方法列表
@end
2、類遵守協議
@interface Dog : NSObject <協議名1,協議名2>
.....
@end
3、協議遵守協議
@protocol 協議名 <協議名1,協議名2>
方法列表
@end
4、協議中聲明方法的關鍵字
@required // 要求實現,不實現會出現警告 ,預設就是這個
@optional // 可選,可實現也可不實現,沒有警告
5、定義變數時,限制這個變數儲存的對象遵守某個協議
類名<協議名> *變數名;
id <協議名> 變數名;
如果沒有遵守對應的協議,編譯器會有警告
6、聲明一個成員變數時可以直接用協議做一個限制
@property (nonatomic, strong) Dog<協議名> *dog;
@property (nonatomic, strong) Person<協議名1, 協議名2> *person;
7、當一個協議比較小,並且只運用到單獨的一個類,那麼可以將協議定義到某個.h檔案(某個類)中。
如果協議用到很多類中,就定義到單獨的.h檔案中。
8、分類@Category定義在單獨的檔案中,也可以定義在原來的類中。
IOS開發學習筆記013-block和protocol