weak 、 strong、delegate为什么用weak、

2018-04-19  本文已影响0人  目前运行时
@interface ViewController ()
@property (strong, nonatomic) myView *testView;
@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    
    myView *testView = [[myView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    testView.backgroundColor = [UIColor redColor];
    [self.view addSubview:testView];
    self.testView = testView;   
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    NSLog(@"点击了界面了");
    [self.testView removeFromSuperview];
    NSLog(@"self.testView 的内存地址 %p",self.testView);
}
@end

#import "myView.h"

@implementation myView

-(void)dealloc{
    NSLog(@"已经被销毁了");
}

@end

然后打印的结果是:


1.png

可以看到 对象并没有销毁,下面用weak我们看一下的结果:


1.png
可以清楚的看到 对象销毁了,这说明强引用的对象在被移除的时候是没有完全移除的,内存地址中还保留着一块内存,只不过该内存地址指向的是空。
在举一个例子 ,我是引用别人的:
1.png

可以看到weak 在对象引用完毕就销毁了。他俩的区别暂时先说到这 ,后面的还有相应的介绍。

self.delegate = self;

假如delegate 用strong修饰的话 那是不是就会导致循环引用了(self 强引用delegate ,delegate又指向了self),所以用weak了。

clang -rewrite-objc 源代码文件名   

看到的结果是:


image.png

可以看到一个局部变量加上__block之后 就会变成与block 一样的结构体,也就是报我们的成员变量拿到地址进行传递过去 ,这样的话block内部就能够修改了。

myView.h文件
#import <UIKit/UIKit.h>
@interface myView : UIView
@property (copy, nonatomic) void (^myBlock)(int x,int y);
@end
 myView.m文件
#import "myView.h"

@implementation myView
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    if (self.myBlock) {
        self.myBlock(10, 10);
    }
}
@end
 ViewController.m文件
#import "ViewController.h"
#import "myView.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    myView *obtainView = [[[NSBundle mainBundle]loadNibNamed:@"myView" owner:nil options:nil] lastObject];
    obtainView.frame = CGRectMake(0, 100, self.view.frame.size.width, 200);
    obtainView.myBlock = ^(int x, int y) {
        int sum = x + y;
        NSLog(@"�%d",sum);
    };
    [self.view addSubview:obtainView];
}

3.block 作为参数进行传递,最常见的是我们封装网络请求 或者写工具类的时候(这之前是我封装网络请求部分的代码)
例如:

        [weakSelf.manager POST:[weakSelf configUrl:weakSelf.requestUrl] parameters:weakSelf.paramDic constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
            [formData appendPartWithFileData:weakSelf.fileData name:weakSelf.name fileName:weakSelf.fileName mimeType:weakSelf.mimeType];
        } progress:^(NSProgress * _Nonnull uploadProgress) {
            CGFloat progress = 1.0 * uploadProgress.completedUnitCount/uploadProgress.totalUnitCount;
            weakSelf.progress = progress;
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            [weakSelf detailSuccess:responseObject complete:complete];
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            [weakSelf detailError:error complete:complete];
        }];

4.blcok可以全局利用(个人理解),倒入头文件全局可以用
例如:

typedef void(^buttonBlock)(int a,int b);

@interface ViewController : UIViewController
@end

5.在方法中调用

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    buttonAction();
}
void (^buttonAction)() = ^{
    NSLog(@"大哥我开始在里边执行方法了");
};
image.png

解释:这里为什么没写:if(buttonAction){buttonAction() };因为blcok我们确定是一定存在的 所以我们没有必要这样写,像1中如果不写if,那么我们在点击myview的时候就会crash。
6.作为函数的返回值(最常用的就是Masonry)作用就是:连式编程

- (MASConstraint * (^)(id attr))mas_equalTo;
- (MASConstraint * (^)(id))equalTo {
    return ^id(id attribute) {
        return self.equalToWithRelation(attribute, NSLayoutRelationEqual);
    };
}
// 实现链式编程
[view1 mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.equalTo(superview.mas_top).with.offset(padding.top); //with is an optional semantic filler
    make.left.equalTo(superview.mas_left).with.offset(padding.left);
    make.bottom.equalTo(superview.mas_bottom).with.offset(-padding.bottom);
    make.right.equalTo(superview.mas_right).with.offset(-padding.right);
}];

-至此我们只是了解blcok的简单使用,更深层次的理解以及使用 我可能写不好了,我找了一篇文章 大家一起进步
block深入理解

上一篇 下一篇

猜你喜欢

热点阅读