美文网首页
2019-05-31 NSCache存储对象分析

2019-05-31 NSCache存储对象分析

作者: 我是小胡胡分胡 | 来源:发表于2019-05-31 10:32 被阅读0次

    nscache,
    1、对于可变的object,nacache持有的object是一个强引用, 变化原来值, objectfokey跟着变化;
    2、对于不可变的object,nacache持有的object还是一个强引用
    3、对于普通对象,nacache持有的object是一个强引用
    4、objectforkey 返回的永远的object与原保存对象地址是相同的,修改其值,会触发cache中的值变化;

    回顾一下, property属性copy,
    1、对于可变string,拷贝会真的发生,两块内存(浅拷贝);
    2、对于不可变string, 虽然是copy, 但是实际上是持有一个强引用, 避免开销一块新的内存!

    
    #import "ViewController.h"
    #import <objc/runtime.h>
    @interface SomeModel:NSObject
    @property (copy, nonatomic) NSString *hello1;
    @property (strong, nonatomic) NSMutableString *hello2;
    @end
    @implementation SomeModel
    -(NSString *)description{
       return [NSString stringWithFormat:@"1=%@,2=%@",self.hello1,self.hello2];
    }
    @end
    
    @interface NSStringSub:NSString
    @end
    @implementation NSStringSub
    
    -(void)dealloc{
       NSLog(@"%s-%d %s",__FILE__,__LINE__, __PRETTY_FUNCTION__);
    }
    
    @end
    
    @interface ViewController ()
    {
       NSCache*cache;
    }
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
       [super viewDidLoad];
       
       cache = [NSCache new];
       
    }
    -(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
       [self test];
    }
    
    
    /**
    nscache,
    1、对于可变的object,nacache持有的object是一个强引用,   变化原来值, objectfokey跟着变化;
    2、对于不可变的object,nacache持有的object还是一个强引用
    3、对于普通对象,nacache持有的object是一个强引用
    4、objectforkey 返回的永远的object与原保存对象地址是相同的,修改其值,会触发cache中的值变化;
    
    
    
    回顾一下,  property属性copy,
       1、对于可变string,拷贝会真的发生,两块内存(浅拷贝);
       2、对于不可变string, 虽然是copy, 但是实际上是持有一个强引用, 避免开销一块新的内存!
    
    
    
    */
    -(void)test{
       //    setObject
       
       //    obj
       //    The object to be stored in the cache.
       //    key
       //    The key with which to associate the value.
       
       
       
       // 1、
       NSString *string =@"12345";
       [cache setObject:string forKey:@"name"];
       NSString *stringfromcache=[cache objectForKey:@"name"];
       NSLog(@"%@", stringfromcache);
       
       //setObject, 是拷贝还是引用
       string = @"1234566778900";
       NSLog(@"%@", [cache objectForKey:@"name"]);
       
       
       // 2、
       NSMutableString *string2=@"12345".mutableCopy;
       [cache setObject:string2 forKey:@"name2"];
       //string2fromcache类型是不可变类型了????
       NSMutableString *string2fromcache =[cache objectForKey:@"name2"];
       NSLog(@"%@, %@--%@", string2fromcache, NSStringFromClass(object_getClass(string2fromcache)),NSStringFromClass(object_getClass(string2)));
       
       // 可以修改--说明操作的仍然是原保存对象的地址内容
       [string2fromcache appendString:@"asdfasdfasdf"];
       //setobject,这里是引用------, 类似于copy,如果是string,就是copy,如果是mutablestring就是strong, 就跟集合类型的属性修饰符一样;  而string属性修饰符copy,遇到string就是strong,遇到不可变就是copy,不可变拷贝的
       
       [string2 appendString:@"asdfasdfasdf"];
       // 上面2处改变, 都会修改cache里面的内容
       NSLog(@"%@", [cache objectForKey:@"name2"]);
       
       
       //    [cache setObject:@"asdfasdfads" forKey:@"name2"];
       //    NSLog(@"%@", string2);
       
       NSMutableString*value2=[cache objectForKey:@"name2"];
       NSLog(@"%s-%d %s",__FILE__,__LINE__, __PRETTY_FUNCTION__);
       
       
       // 3、
       NSStringSub*string3=[@"123132" substringFromIndex:1];
       
       [cache setObject:string3 forKey:@"name3"];
       
       
       
       // 4、
       NSMutableString*stringsub=@"123".mutableCopy;
       NSMutableArray*arr=@[@"1",@"2",@"3",stringsub].mutableCopy;
       //设置可变对象value
       [cache setObject:arr forKey:@"name4"];
       [arr addObject:@"5"];
       //返回的是不可变类型的---[cache objectForKey:@"name4"]
       NSMutableArray *arrfromcache=[cache objectForKey:@"name4"];
       NSLog(@"%@", arrfromcache);
       //此处可以修改,  修改后,会更新cache中的内容
       [arrfromcache addObject:@"123"];
       NSLog(@"%@", arrfromcache);
       NSLog(@"%@", arr);
       NSMutableArray *arrfromcache2=[cache objectForKey:@"name4"];
       NSLog(@"%@", arrfromcache2);
       
       NSMutableString*tempMustring=  [arr objectAtIndex:3];
       [tempMustring appendString:@"999999"];
       NSLog(@"%@", [cache objectForKey:@"name4"]);
       NSArray *resultarray = [cache objectForKey:@"name4"];
       NSLog(@"%@", [resultarray class]);
       NSArray *result2 =[[cache objectForKey:@"name4"] mutableCopy];
       NSMutableString*tempMustring2=  [result2 objectAtIndex:3];
       [tempMustring2 appendString:@"88888"];
       NSLog(@"%@", [cache objectForKey:@"name4"]);
       
       // 5、
       NSArray *array3=@[@"1",@"2".mutableCopy];
       [cache setObject:array3 forKey:@"name5"];
       NSMutableString*string31= array3[1];
       [string31 appendString:@"22222222"];
       NSLog(@"%@", [cache objectForKey:@"name5"]);
       
       
       // 6、
       SomeModel *obj = [SomeModel new];
       obj.hello1 = @"12345";
       obj.hello2 = @"abc".mutableCopy;
       [cache setObject:obj forKey:@"name6"];
       SomeModel *obj2= [cache objectForKey:@"name6"];
       NSLog(@"%@", obj2);
       
       obj.hello1 = @"1234567890";
       [obj.hello2 appendString:@"123456"];
       SomeModel *obj3 = [cache objectForKey:@"name6"];
       NSLog(@"%@",obj3);
    }
    
    
    @end
    
    

    相关文章

      网友评论

          本文标题:2019-05-31 NSCache存储对象分析

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