美文网首页iOS LearningiOS开发
iOS_WeakSelf&&StrongSelf

iOS_WeakSelf&&StrongSelf

作者: 陈胜华 | 来源:发表于2016-03-22 17:38 被阅读2246次

    1.普通用法

    1.weakSelf:block内部使用一次,引用计数不加1,self释放,weakSelf = nil

    LoginViewController *loginVC = [[LoginViewController alloc]init];
    __weak __typeof(self) weakSelf = self;
    loginVC.loginFinished = ^(){
        [weakSelf doSomeThing];//使用多次的话,就需要strongSelf
    };
    [self.navigationController pushViewController:loginVC animated:YES];
    
    

    2.strongSelf

    LoginViewController *loginVC = [[LoginViewController alloc]init];
    __weak __typeof(self) weakSelf = self;//block内部多次使用
    loginVC.loginFinished = ^(){
        __strong __typeof(self) strongSelf = weakSelf;
        [strongSelf doSomeThing];
        [strongSelf doOtherThing];
    };
    [self.navigationController pushViewController:loginVC animated:YES];
    
    

    2.宏定义用法

    1.定义宏:使用前提条件,定义一下宏

    #define WeakSelf(weakSelf)      __weak __typeof(&*self)    weakSelf  = self;
    #define StrongSelf(strongSelf)  __strong __typeof(&*self) strongSelf = weakSelf;
    
    //weakSelf宏用法
    /**修改用户密码*/
    - (void)requestForModyPasswordWithCode:(NSString*)messageCode mobile:(NSString*)mobile newPasswrod:(NSString*)newPassword {
    
        WeakSelf(weakSelf)
        [[TJProgressHud shareInstance]showProgressView:@"正在修改密码..."];
        [[TJNetService shareInstance] modifyPasswordWithCode:messageCode mobile:mobile passowrd:newPassword Success:^(id result) {
            /**
              .
              .
            */
            [weakSelf.navigationController popToRootViewControllerAnimated:YES];
        } Failure:^(id result) {
            [[TJProgressHud shareInstance]removeSVProgress];
            [TJToastView showWithText:@"修改密码失败,请稍候尝试" topOffset:MainScreenSize.height/2.0];
        }];
    }
    
    
    //strongSelf宏用法
    /**发送短信验证码*/
    - (void)requestForSendMessageCodeByPhoneNumber:(NSString*)mobile{
        WeakSelf(weakSelf)
        [[TJNetService shareInstance] sendCodeOnForgetPwdWithMobile:mobile  Success:^(id result) {
            StrongSelf(strongSelf)
            [strongSelf.evbtnSendMessage setUserInteractionEnabled:NO];
            NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                                              target:strongSelf
                                                            selector:@selector(_efUpdateButtonStatus:)
                                                            userInfo:nil repeats:YES];
            [timer fire];
        } Failure:^(id result) {
            [TJToastView showWithText:@"验证码发送失败,请稍后尝试" topOffset:140];
        }];
    }
    
    - (void)_efUpdateButtonStatus:(NSTimer*)timer{
      //do someThing...
    }
    
    

    相关文章

      网友评论

      • 跑调的安眠曲:block内部使用一次,假设那个doSomeThing方法中又调用了很多self属性或者方法,岂不是在执行过程中,也会crash?
        Jason_a022:@荆楼的东河 以前的小bug 现在随便哪种写法都可以 已经优化了
        荆楼的东河:关于宏定义,#define WeakSelf(weakSelf) __weak __typeof(&*self) weakSelf = self;
        &*的道理何在,望赐教。
        陈胜华:首先,crash是不会的,最多也就循环引用,内存得不到释放;其次,已经应用了strongSelf,这时候,self的方法已经成了弱引用了,所以就没关系了

      本文标题:iOS_WeakSelf&&StrongSelf

      本文链接:https://www.haomeiwen.com/subject/dckllttx.html