IOS开发基础之OC的Block入门_Day09-Block
生活随笔
收集整理的這篇文章主要介紹了
IOS开发基础之OC的Block入门_Day09-Block
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
IOS開發基礎之OC的Block入門_Day09-Block
block是oc的重要的基礎知識,重點之重。跟協議一樣重要,是進行函數回調重要手段。在后續的UI學習具有舉足輕重的地位。學會基礎的block,夯實基礎,有的放矢。
第一天
第2個項目
// // main.m // block作為函數的參數 // // Created by 魯軍 on 2021/4/24. // /**block 是1個數據類型,能不能作為參數呢,可以2 如何為函數定義block 類型的參數a. 就是在小括號中聲明1個指定格式的block變量就可以了b 可以使用typedef 簡化定義,這樣看起來就不會暈3 如果調用帶block的參數的函數呢a.如果要調用的函數的參數是block類型的,那么要求在調用的時候傳入1個和形參block要求的代碼段;b .調用的時候,可以先將代碼段存儲到1個block變量中,然后再傳遞這個block變量,也可以直接將符合要求的代碼段寫在小括弧中c .小技巧,通過xcode t提示自動生成代碼段4 將block 作為函數的參數可以實現什么樣的效果 可以將調用者自己寫1段代碼 傳遞到 函數內部區去執行*/ #import <Foundation/Foundation.h> typedef void (^NewType)(); //為這個參數定義1個參數,定義1個block類型的參數 //無參數無返回值的block void test(NewType block1){NSLog(@"~~~~~~~~~~");block1();NSLog(@"~~~~~~~~~~"); } void test2(int (^paramsBlock)(int num1,int num2)){NSLog(@"~~~~~~~~~~");int sum = paramsBlock(10,20);NSLog(@"sum = %d",sum);NSLog(@"~~~~~~~~~~"); } int main(int argc, const char * argv[]) {NewType type1 = ^(void){NSLog(@"哈哈");NSLog(@"😄");NSLog(@"😁");};test(type1);test(^(void){NSLog(@"哈哈");NSLog(@"😄");NSLog(@"😁");});test(^{NSLog(@"哈哈");NSLog(@"😄");NSLog(@"😁");});test2(^int(int num1, int num2) {return num1 + num2;});test(^{NSLog(@"我愛廣州小蠻腰");NSLog(@"北京天安門不愛");});return 0; }第3個項目
// // main.m // 05-練習 // // Created by 魯軍 on 2021/4/24. // /*當方法內部需要執行1個功能,但是這個功能不確定那么這個時候,就使用block讓調用者將這個功能的具體實現傳遞進來*/ #import <Foundation/Foundation.h> #import "CZArray.h" int main(int argc, const char * argv[]) {char* contries[] = {"China","Jpanese","Aisa","India","Laos","Turkey","Vietnam","Bdqweqrwfddsfsda"};CZArray *a =[CZArray new];[a sortWIthCountries:contries andLength:sizeof(contries) / 8];/**2021-04-24 15:23:04.091049+0800 05-練習[2650:65152] Aisa2021-04-24 15:23:04.091645+0800 05-練習[2650:65152] China2021-04-24 15:23:04.091882+0800 05-練習[2650:65152] India2021-04-24 15:23:04.091998+0800 05-練習[2650:65152] Jpanese2021-04-24 15:23:04.092098+0800 05-練習[2650:65152] Laos2021-04-24 15:23:04.092126+0800 05-練習[2650:65152] Turkey2021-04-24 15:23:04.092144+0800 05-練習[2650:65152] Vietnam*/[a sortWIthCountries:contries andLength:(sizeof(contries) / 8) andCompareBlock:^BOOL(char *country1, char *country2) {int res = (int)strlen(country1)-(int)strlen(country2);if(res>0){return YES;}return NO;}];for(int i=0;i<sizeof(contries)/8;i++){NSLog(@"%s",contries[i]);}NSLog(@"~~~~~~~~~~~~~~~~~~~");[a sortWIthCountries:contries andLength:(sizeof(contries) / 8) andCompareBlock:^BOOL(char *country1, char *country2) {int res = strcmp(country1, country2);return res > 0;}];for(int i=0;i<sizeof(contries)/8;i++){NSLog(@"%s",contries[i]);}return 0; } // // CZArray.h // 05-練習 // // Created by 魯軍 on 2021/4/24. //#import <Foundation/Foundation.h> typedef BOOL (^NewType)(char *country1,char *country2); NS_ASSUME_NONNULL_BEGIN @interface CZArray : NSObject -(void) sortWIthCountries:(char*[])countries andLength:(int)len; -(void) sortWIthCountries:(char*[])countries andLength:(int)len andCompareBlock:(NewType) compareBlock; @endNS_ASSUME_NONNULL_END // // CZArray.m // 05-練習 // // Created by 魯軍 on 2021/4/24. //#import "CZArray.h" #import <string.h> @implementation CZArray - (void)sortWIthCountries:(char * _Nonnull *)countries andLength:(int)len{for(int i = 0;i<len-1;i++){for(int j=0;j<len-1-i;j++){int res = strcmp(countries[j], countries[j+1]);if(res>0){char* temp = countries[j];countries[j] = countries[j+1];countries[j+1] = temp;}}} } - (void)sortWIthCountries:(char * _Nonnull *)countries andLength:(int)len andCompareBlock:(NewType)compareBlock{for(int i = 0;i<len-1;i++){for(int j=0;j<len-1-i;j++){ // int res = strcmp(countries[j], countries[j+1]);BOOL res = compareBlock(countries[j], countries[j+1]);if(res==YES){char* temp = countries[j];countries[j] = countries[j+1];countries[j+1] = temp;}}} } @end第4個項目
// // main.m // 06-練習2 // // Created by 魯軍 on 2021/4/24. //#import <Foundation/Foundation.h> #import "CZArray.h" typedef void (^NewType)(); NewType ttt(){void (^block1)() = ^{NSLog(@"123dfsd");};return block1; }int main(int argc, const char * argv[]) {CZArray *a = [CZArray new];[a bianLiWithBlock:^(int val) {NSLog(@"%d",val + 1);}];NewType type = ttt();type();return 0; } // // CZArray.h // 06-練習2 // // Created by 魯軍 on 2021/4/24. //#import <Foundation/Foundation.h>NS_ASSUME_NONNULL_BEGIN@interface CZArray : NSObject {int _arr[10]; } -(void)bianLiWithBlock:(void(^)(int val))processBlock; @endNS_ASSUME_NONNULL_END // // CZArray.m // 06-練習2 // // Created by 魯軍 on 2021/4/24. //#import "CZArray.h"@implementation CZArray - (instancetype)init {self = [super init];if (self) {for(int i=1;i<11;i++){_arr[i-1] = i *10;}}return self; } -(void)bianLiWithBlock:(void(^)(int val))processBlock{for(int i=0;i<10;i++){processBlock(_arr[i]);}} @end總結
以上是生活随笔為你收集整理的IOS开发基础之OC的Block入门_Day09-Block的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 你很棒的---自我管理方法,一生受用!!
- 下一篇: 基于OpenCL的mean filter