zoukankan      html  css  js  c++  java
  • Object-C-复制

    copy 减少对象上下文依赖
    copy 创建一个新对象,copy得到的副本对象与原来内容相同,新的对象retain为1,与旧有对象的引用计数无关,旧有对象没有变化
    使用 copy 创建出来的对象是不可变的, 使用mutableCopy创建出来是可以改变的
    如果对不可变对象复制,copy是指复制(浅拷贝)和mutableCopy就是对象复制(深拷贝)。如果是对可变对象复制,都是深拷贝,但是copy返回的对象是不可变的;

    对于属性@Property的copy 修饰符
    属性的copy特性会在属性调用Setter时发送copy消息;
    属性并没有mutableCopy特性;
    字符串属性一般设置为copy,目的是减少对于可变字符串对象的依赖。
    字符串类型一般使用copy
    自定义的类要添加这个协议 并实现copyWithZone这个方法
    ie:
    -(id)copyWithZone:(NSZone *)zone{
    XYPoint *point=[[XYPoint allocWithZone:zone]initWithX:self.x andY:self.y];
    return point;
    }
    对象复制
    ie:
    NSMutableArray *arr=[NSMutableArray array];
    NSString *str1=@"one";
    NSString *str2=@"two";
    NSString *str3=@"three";
    [arr addObject:str1];
    [arr addObject:str2];
    [arr addObject:str3];
    NSArry *copyArr=[arr copy];
    arr与copyArr地址不同
    ie 改为NSMutablesString
    NSMutableArray *arr=[NSMutableArray array];
    NSString *str1=[[NSMutableArray array];
    NSString *str2=@"two";
    NSString *str3=@"three";
    [arr addObject:str1];
    [arr addObject:str2];
    [arr addObject:str3];
    NSArry *copyArr=[arr copy];
    arr与copyArr地址不同
    NSMutableArray *arr=[NSMutableArray array];
    NSMutableString *str1=[[NSMutableString alloc] initWithString:@"one"];
    NSMutableString *str2=[[NSMutableString alloc] initWithString:@"tow"];
    NSMutableString *str3=[[NSMutableString alloc]initWithString:@"three"];
    [arr addObject:str1];
    [arr addObject:str2];
    [arr addObject:str3];
    NSArray *copyArr=[arr copy];
    NSLog(@"arr address = %p",arr);
    NSLog(@"copyArr address = %p",copyArr);
    NSMutableString *firstStr=[arr objectAtIndex:0];
    [firstStr appendString:@"1"];
    NSLog(@"%@",arr);
    NSLog(@"%@",copyArr);
    打印出来两个数组的内容都同时改变了
    浅拷贝
    arr1 对象 arr2

    0->    A     <-0
    1->    B     <-1
    2->    C     <-2
    3->    D     <-3
    4->    E     <-4
    
    NSMutableArray *arr=[NSMutableArray array];
    NSMutableString *str1=[[NSMutableString alloc] initWithString:@"one"];
    NSMutableString *str2=[[NSMutableString alloc] initWithString:@"tow"];
    NSMutableString *str3=[[NSMutableString alloc]initWithString:@"three"];
    [arr addObject:str1];
    [arr addObject:str2];
    [arr addObject:str3];
    NSArray *copyarr=[[NSArray alloc]initWithArray:arr copyItems:YES];
    NSMutableString *firstStr=[arr objectAtIndex:0];
    [firstStr appendString:@"1"];
    深拷贝
    arr1   对象     arr2  对象
    0->    A        0->   A
    1->    B        1->   B
    2->    C        2->   C
    id:
    NSMutableArray *arr=[NSMutableArray array];
    XYpoint *p1=[[XYPoint alloc]initWithX:1 andY:2];
    XYpoint *p2=[[XYPoint alloc]initWithX:1 andY:3];
    XYpoint *p3=[[XYPoint alloc]initWithX:1 andY:4];
    NSArray *copyArr = [[NSArray alloc]initWithArray:arr copyItems:YES];
    XYPoint *point = [arr objectAtIndex:0];
    point.x=7;
  • 相关阅读:
    vue 爬坑之路---设置背景图,不能resolve编译
    vue 爬坑之路----flexible.js不适配ipad和ipad pro
    vue-cli3 vue.config.js配置
    vue组件
    Vue 路由按需keep-alive
    vue-cli项目搭建
    http和https
    js时间戳和日期互转换
    vue补充
    js实现头像上传(移动端,PC端均可)
  • 原文地址:https://www.cnblogs.com/Opaser/p/4561861.html
Copyright © 2011-2022 走看看