• Block 代码快


    1.原理

      1.1block类型  

         a.全局bock块   贯彻整个程序

         b.栈块  存在于栈内存中,作用域中使用

         c.堆块  自行管理其内存

         注*:http://blog.parse.com/learn/engineering/objective-c-blocks-quiz/   检验自己了解情况

      1.2定义

          类函数,闭包

          注*:1.block捕获块中出现的外部变量

                   2.栈块copy之后会形成堆块

                   3.__weak(ARC)或__block(MRC)来避免循环引用

                   4.若想深入了解可使用C++理解(clang -rewrite-objc 源代码文件名)  

    2.使用

         函数和block对比进行

         1.调用 

    - (void)blockImp{
        
        //只有调用Block时候,才会执行其{}体内的代码
        [self block1];
    }
    
    - (void)block1{
        void (^mm)(int) = ^(int index){
            NSLog(@"block1 index = %d",index);
        };
        if (mm) {
            mm(23);
        }
    }
    - (void)functionUser{
        function1("Mr huang"); //简单函数调用
    }
    void function1(char * name){
        printf("Hello %s
    ",name);
    }

       2.形参

    - (void)blockImp{
        
        [self block2:^(int index) {
            NSLog(@"block2 index = %d",index);
        }];
        
        //函数定义不能在 方法内部,而block可以。
        [self block2:printNumBlock];
    }
    
    - (void)block2:(void(^)(int))block{
        if (block) {
            block(24);
        }
    }
    
    void(^printNumBlock)(int) = ^(int num){
        NSLog(@"int number is %d",num);
    };
    int sayHello(int m){
        printf("sayHello %d
    ",m);
        return m + 1;
    }
    
    - (void)functionUser{
        function2(sayHello); //函数作为参数掉用
    }

        3.返回值

    - (void)blockImp{
        [self block3](25);
    }
    
    - (void(^)(int))block3{
        return ^(int index){
            NSLog(@"block3 index = %d",index);
        };
    }
    int add(int a , int b){
        return a + b;
    }
    
    int sub(int a , int b){
        return  a - b;
    }
    
    void * function3(int i){
        if (i == 1) {
            return add;
        }else{
            return sub;
        }
    }
    
    - (void)functionUser{
        int (*m)(int, int)  = function3(2); //函数作为参数
        int ok = m(1,2);
        printf("ok %d
    ",ok);
    }

    3.链式编程

        3.1.第三方链式 (Masory)

        3.2.自定义

    #import <Foundation/Foundation.h>
    
    @interface GLPerson : NSObject
    
     @property(nonatomic , assign)NSInteger age;
    
    @property(nonatomic , copy)NSString * name;
    
    - (GLPerson *((^)(NSString *)))setName;
    
    - (GLPerson *((^)(NSInteger)))setAge;
    
    @end
    #import "GLPerson.h"
    
    @implementation GLPerson
    
    -(NSString *)description{
        return [NSString stringWithFormat:@"name = '%@';age = %ld",_name,_age];
    }
    
    - (GLPerson *((^)(NSString *)))setName{
        return ^(NSString * name){
            self.name = name;
            return self;
        };
    }
    
    - (GLPerson *((^)(NSInteger)))setAge{
        return ^(NSInteger age){
            self.age = age;
            return self;
        };
    }
    
    @end
        GLPerson *person = [[GLPerson alloc] init];
        person.setAge(12).setName(@"gulong");
        NSLog(@"%@",person);
  • 相关阅读:
    slim中返回结果加密的
    windows下的redis和redismyadmin
    centos6.2升级到centos6.8(6.5应该也一样)
    剑指offer-二叉树的深度
    leetcode-【hard】4. Median of Two Sorted Arrays
    leetcode-【中等题】5. Longest Palindromic Substring
    leetcode-【中等题】3. Longest Substring Without Repeating Characters
    leetcode-【中等题】2. Add Two Numbers
    leetcode-【中等题】228. Summary Ranges
    leetcode-【hard】273. Integer to English Words
  • 原文地址:https://www.cnblogs.com/gulong/p/5554191.html
Copyright © 2020-2023  润新知