美文网首页
iOS 实践出真知-《强引用与弱引用》

iOS 实践出真知-《强引用与弱引用》

作者: 蜂子阁先生 | 来源:发表于2018-10-22 17:02 被阅读11次

    码上说

    SWItem.h

    //
    //  SWItem.h
    //  BaseLesson
    //
    //  Created by yh on 2018/10/10.
    //  Copyright © 2018年 yh. All rights reserved.
    //
    
    #import <Foundation/Foundation.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface SWItem : NSObject
    
    @end
    
    NS_ASSUME_NONNULL_END
    
    

    SWItem.m

    //
    //  SWItem.m
    //  BaseLesson
    //
    //  Created by yh on 2018/10/10.
    //  Copyright © 2018年 yh. All rights reserved.
    //
    
    #import "SWItem.h"
    
    @implementation SWItem
    
    
    - (void)dealloc
    {
        NSLog(@"对象已被释放");
    }
    
    @end
    
    

    ViewController.h

    //
    //  ViewController.h
    //  BaseLesson
    //
    //  Created by yh on 2018/10/10.
    //  Copyright © 2018年 yh. All rights reserved.
    //
    
    #import <UIKit/UIKit.h>
    
    @interface ViewController : UIViewController
    
    
    @end
    
    
    

    ViewController.m

    //
    //  ViewController.m
    //  BaseLesson
    //
    //  Created by yh on 2018/10/10.
    //  Copyright © 2018年 yh. All rights reserved.
    //
    
    #import "ViewController.h"
    #import "SWItem.h"
    @interface ViewController ()
    
    @property (nonatomic,strong) SWItem *strongItem;
    
    @property (nonatomic,strong) SWItem *strongItem2;
    
    @property (nonatomic,weak) SWItem *weakItem;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    //    [self test1];
        
        [self test4];
    
    }
    
    - (void)test1{
        //① 弱引用测试
        //[[SWItem alloc] init] 这个函数 开辟内存 _weakItem 是弱指针 由于没有强指针引用这个对象,会被立马释放,所以 weakItem 也为nil
        _weakItem = [[SWItem alloc] init];
        NSLog(@"_weakItem is %@",_weakItem);
    }
    
    - (void)test2{
        //er 强引用测试 [[SWItem alloc] init] 这个函数 开辟内存,
        
        // _strongItem 强引用 指向该内存 该块内存不会被释放
        _strongItem =  [[SWItem alloc] init];
        
        //由于 有强引用指向该块内存 _weakItem 是有值的
        _weakItem = _strongItem;
        NSLog(@"_weakItem is %@",_weakItem);
    }
    
    
    
    - (void)test3{
        //er 强引用测试 [[SWItem alloc] init] 这个函数 开辟内存,
        
        // _strongItem 强引用 指向该内存 该块内存不会被释放
        _strongItem =  [[SWItem alloc] init];
        
        //由于 有强引用指向该块内存 _weakItem 是有值的
        _weakItem = _strongItem;
        
        // _strongItem 设置为 nil 后 没有强引用指向该块内存 ,内存被释放
        _strongItem = nil;
        
        NSLog(@"_weakItem is %@",_weakItem);
    }
    
    
    - (void)test4{
        //er 强引用测试 [[SWItem alloc] init] 这个函数 开辟内存,
        
        // _strongItem 强引用 指向该内存 该块内存不会被释放
        _strongItem =  [[SWItem alloc] init];
        
        //由于 有强引用指向该块内存 _weakItem 是有值的
        _weakItem = _strongItem;
        
        // _strongItem2 强引用 指向该内存 该块内存不会被释放
        _strongItem2 = _strongItem;
        
        // _strongItem 设置为 nil
        _strongItem = nil;
        
        //_strongItem 虽然已经不指向 该块内存 但 _strongItem2 依旧强引用该块内存没有被释放 ,弱引依旧存在值
        NSLog(@"_weakItem is %@",_weakItem);
    }
    
    
    @end
    
    

    strong weak 功能说明

    首先说一下对象的释放原则,如果申请了某个对象,开辟了相关内存,却没有强指针指向它,也就是强引用,那这个对象会被释放

    相关文章

      网友评论

          本文标题:iOS 实践出真知-《强引用与弱引用》

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