OC,oc語言

來源:互聯網
上載者:User

OC,oc語言

一、block

1> 基本使用

  • 相當於用來存放代碼的代碼塊
  • 效率高
  • 若沒有形參可以省略小括弧

2> block與函數的相同點

  • 可以儲存代碼
  • 可以有傳回值
  • 可以有形參
  • 調用方式一樣

3> block對外部變數的訪問

  • 可以訪問外部變數
  • 預設情況下,不能在block內修改外部變數
  • 用關鍵字__block修飾外部變數,就可以在block內修改它

4> 用typedef定義block類型與函數類型

5> 樣本

/* 1.使用typedef定義分別一個block類型,一個指向函數的指標 2.分別定義一個求和函數,用函數指標指向它 3.用新的block類型定義一個求和的block 4.定義一個輸出一條橫下的block 5.分別用函數指標和block實現兩個數的求和,中間輸出一條橫線 */#import <Foundation/Foundation.h>//用typedef定義一個傳回值類型為int,有兩個int類型形參的block類型typedef int (^myBlock)(int, int);//用typedef定義一個傳回值類型為int,有兩個int類型形參的函數指標typedef int (*sumP)(int, int);//定義一個求和函數int sum(int a, int b){    return a + b;}int main(){    //使用新的block類型定義一個求和的block    myBlock sumBlock =^(int a, int b){        return a + b;    };    //定義一個輸出橫線的block    void (^lineBLock)() = ^{        NSLog(@"--------------");    };        //使函數指標指向求和函數    sumP p = sum;    //通過block輸出兩個函數的和    NSLog(@"%d", sumBlock(11, 12));    //通過block輸出一條橫線    lineBLock();    //通過函數指標輸出兩個數的和    NSLog(@"%d", sumBlock(11, 12));        return 0;}

 

二、protocol

1> 基本使用

2> 協議遵守

3> @property的參數

4> 協議與分類

5> 協議的應用

6> 樣本

/******main.m檔案******/#import <Foundation/Foundation.h>#import "Parent.h"#import "Children.h"#import "Nanny.h"int main(){    @autoreleasepool {                //定義一個Parent對象        Parent *p = [[Parent alloc] init];        //定義一個Children對象        Children *c = [[Children alloc] init];        //定義一個Nanny對象        Nanny *n = [[Nanny alloc] init];                //將對象p的屬性Children初始化為c        p.children = c;        //將對象p的屬性nanny初始化為n        p.nanny = n;                //調用對象p的takeCareTheChildren方法        [p takeCareTheChildren];            }    return 0;}/******Parent.h檔案******/#import <Foundation/Foundation.h>//#import "LookAfterTheChildren.h"//#import "Children.h"#import "Nanny.h"@interface Parent : NSObject//聲明成員變數nanny,且限定其只能接收遵守LookAfterTheChildren協議的對象@property (nonatomic, strong) id<LookAfterTheChildren> nanny;//聲明成員變數children@property (nonatomic, strong) Children *children;//定義方法,實現照顧children的功能- (void)takeCareTheChildren;@end/******Parent.m檔案******/#import "Parent.h"@implementation Parent//- (void)takeCareTheChildren{    //委託代理nanny來完成照顧children的任務    [_nanny feedTheChildren];    [_nanny playWithTheChildren];}@end/******Children.h檔案******/#import <Foundation/Foundation.h>@interface Children : NSObject@end/******Children.m檔案******/#import "Children.h"@implementation Children@end/******Nanny.h檔案******/#import <Foundation/Foundation.h>//#import "LookAfterTheChildren.h"//要想成為Parent的代理,必須遵守LookAfterTheChildren協議@interface Nanny : NSObject<LookAfterTheChildren>@end/******Nanny.m檔案******/#import "Nanny.h"@implementation Nanny//實現LookAfterTheChildren協議的方法,具備照顧children的能力- (void)feedTheChildren{    NSLog(@"喂孩子吃飯");}- (void)playWithTheChildren{    NSLog(@"陪孩子玩耍");}@end/******LookAfterTheChildren.h檔案******/#import <Foundation/Foundation.h>//由Parent定義的協議,聲明照顧children的方法列表@protocol LookAfterTheChildren <NSObject>//給children喂飯的方法- (void)feedTheChildren;//陪children玩耍的方法- (void)playWithTheChildren;@end

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.