Objective-C Copy

Copy

1、NSString

1、[不可变对象 copy] :通过指针拷贝得到一个不可变对象。
2、[不可变对象 mutableCopy] :通过内容拷贝得到一个可变对象。

-(void) TestNSString
{
    NSString *str0=@"hello";
    NSString *str1=str0;        //指针拷贝
    NSString *str2=[str0 copy]; //指针拷贝
    NSString *str3=[str0 mutableCopy]; //内容拷贝
    NSMutableString *str4=[str0 mutableCopy]; //内容拷贝
   
    NSLog(@"%p",str0);
    NSLog(@"%p",str1);
    NSLog(@"%p",str2);
    NSLog(@"%p",str3);
    NSLog(@"%p",str4);
    
    [str4 appendString:@"xxx"];
    NSLog(@"%@",str4);
    
    /*
     2019-02-24 10:08:19.660505+0800 JJ[5811:673370] 0x106a710b0
     2019-02-24 10:08:19.660647+0800 JJ[5811:673370] 0x106a710b0
     2019-02-24 10:08:19.660750+0800 JJ[5811:673370] 0x106a710b0
     2019-02-24 10:08:19.660850+0800 JJ[5811:673370] 0x60000288c750
     2019-02-24 10:08:19.660938+0800 JJ[5811:673370] 0x60000288ca20
     2019-02-24 10:08:19.661114+0800 JJ[5811:673370] helloxxx
     */

}

2、NSMutableString

1、[可变对象 copy] :通过内容拷贝得到一个不可变对象。
2、[可变对象 mutableCopy] :通过内容拷贝得到一个可变对象。

-(void)testMutableString
{
    NSMutableString *mstr0=[NSMutableString stringWithString:@"world"];
    NSMutableString *mstr1=mstr0; //指针拷贝
    NSMutableString *mstr2=[mstr0 mutableCopy];//内容拷贝
    NSMutableString *mstr3=[mstr0 copy];//内容拷贝

    NSLog(@"%p",mstr0);
    NSLog(@"%p",mstr1);
    NSLog(@"%p",mstr2);
    NSLog(@"%p",mstr3);
       
    @try{
        [mstr2 appendString:@"s"];
        NSLog(@"success2 %@",mstr2);
    }@catch(NSException *error)
    {
        NSLog(@"error2");
    }
    @try{
        [mstr3 appendString:@"4"];
        NSLog(@"success3 %@",mstr3);
    }@catch(NSException *error)
    {
        NSLog(@"error3");
    }
    /*
     2019-02-24 10:16:08.549743+0800 JJ[5875:685020] 0x600001e337e0
     2019-02-24 10:16:08.549898+0800 JJ[5875:685020] 0x600001e337e0
     2019-02-24 10:16:08.549995+0800 JJ[5875:685020] 0x600001e33ae0
     2019-02-24 10:16:08.550077+0800 JJ[5875:685020] 0xdf411deadb817004
     2019-02-24 10:16:08.550165+0800 JJ[5875:685020] success2
     2019-02-24 10:16:08.550365+0800 JJ[5875:685020] -[NSTaggedPointerString appendString:]: unrecognized selector sent to instance 0xdf411deadb817004
     2019-02-24 10:16:08.550789+0800 JJ[5875:685020] error3
     */
}

3、NSArray

1、[不可变对象 copy] :通过指针拷贝得到一个不可变对象。
2、[不可变对象 mutableCopy] :通过内容拷贝得到一个可变对象。

-(void)testNSArrary{
    NSArray *ar0=@[@1,@2];
    NSArray *ar1=ar0;
    NSArray *ar2=[ar0 copy];
    NSMutableArray *ar3=[ar0 mutableCopy];
    
    NSLog(@"%p",ar0);
    NSLog(@"%p",ar1);
    NSLog(@"%p",ar2);
    NSLog(@"%p",ar3);

    /*
     2019-02-23 20:32:14.977070+0800 JJ[2561:316854] 0x6000030cd4c0
     2019-02-23 20:32:14.977219+0800 JJ[2561:316854] 0x6000030cd4c0
     2019-02-23 20:32:14.977308+0800 JJ[2561:316854] 0x6000030cd4c0
     2019-02-23 20:32:14.977391+0800 JJ[2561:316854] 0x600003e9b8a0
     */
}

4、NSMutableArray

1、[可变对象 copy] :通过内容拷贝得到一个不可变对象。
2、[可变对象 mutableCopy] :通过内容拷贝得到一个可变对象。
3、容器类对象单层内容拷贝。

-(void)testNSMutableArrary
{
    NSMutableArray *element = [NSMutableArray arrayWithObject:@1];
    NSMutableArray *mar0= [NSMutableArray arrayWithObject:element];
    NSMutableArray *mar1=mar0;
    NSMutableArray *mar2=[mar0 mutableCopy];
    NSMutableArray *mar3=[mar0 copy];
    NSLog(@"%p",mar0);
    NSLog(@"%p",mar1);
    NSLog(@"%p",mar2);
    NSLog(@"%p",mar3);

    [mar2[0] addObject:@"xml"];//第二层
    [mar2 addObject:@"99"]; //第一层
    NSLog(@"%@",mar0);
    NSLog(@"%@",mar1);
    NSLog(@"%@",mar2);
    NSLog(@"%@",mar3);
    
    /*
     2019-02-24 10:34:28.890719+0800 JJ[6040:710731] 0x600000808ae0
     2019-02-24 10:34:28.890863+0800 JJ[6040:710731] 0x600000808ae0
     2019-02-24 10:34:28.890963+0800 JJ[6040:710731] 0x6000008085a0
     2019-02-24 10:34:28.891058+0800 JJ[6040:710731] 0x600000450d50
     2019-02-24 10:34:28.891250+0800 JJ[6040:710731] (
        (
           1,
           xml
        )
     )
     2019-02-24 10:34:28.891366+0800 JJ[6040:710731] (
        (   1,
          xml 
        )
     )
     2019-02-24 10:34:28.891477+0800 JJ[6040:710731] (
     (
         1,
         xml
     ),
     99
     )
     2019-02-24 10:34:28.891619+0800 JJ[6040:710731] (
         (
             1,
            xml
          )
     )
     */
}

5、属性中Copy

1、copy:(通过指针拷贝)产生一个不变对象的不可变副本赋值给该属性,该属性不会因为原对象改变而改变(因为:原对象不可变)
2、strong:(通过指针拷贝)指向一个不变对象,该属性不会因为原对象改变而改变(因为:原对象不可变)

@property (nonatomic,copy) NSString *testCopy;
@property (nonatomic,strong) NSString *testStrong;

-(void)testProperty
{
    NSString *str=@"hello";
    testCopy=str;
    testStrong=str;
    
    NSLog(@"%p",str);
    NSLog(@"%p",testCopy);
    NSLog(@"%p",testStrong);

    /*
     2019-02-24 10:52:37.854787+0800 JJ[6208:737603] 0x10e5610b0
     2019-02-24 10:52:37.854935+0800 JJ[6208:737603] 0x10e5610b0
     2019-02-24 10:52:37.855032+0800 JJ[6208:737603] 0x10e5610b0
     */    
}

3、copy:(通过指针拷贝)产生一个可变对象的不可变副本赋值给该属性,属性值会因原对象变化而改变
4、strong:(通过指针拷贝)指向一个可变对象,属性值会因原对象变化而改变

-(void)testProperty2
{
    NSMutableString *str=[NSMutableString stringWithFormat:@"hello"];
    testCopy=str;
    testStrong=str;

    NSLog(@"%p",str);
    NSLog(@"%p",testCopy);
    NSLog(@"%p",testStrong);

    [str appendString:@"world"];
    
    NSLog(@"%@",str);
    NSLog(@"%@",testCopy);
    NSLog(@"%@",testStrong);

    /*
     2019-02-24 11:43:21.742164+0800 JJ[6648:803871] 0x600002661800
     2019-02-24 11:43:21.742309+0800 JJ[6648:803871] 0x600002661800
     2019-02-24 11:43:21.742407+0800 JJ[6648:803871] 0x600002661800
     2019-02-24 11:43:21.742512+0800 JJ[6648:803871] helloworld
     2019-02-24 11:43:21.742611+0800 JJ[6648:803871] helloworld
     2019-02-24 11:43:21.742700+0800 JJ[6648:803871] helloworld
     */
    
}

6、NSCoping

1.基类如果实现了NSCopying或NSMutableCopying协议,

类ClassA:

@interface ClassA : NSObject<NSCopying>
@property(nonatomic,copy)NSString *x;
@end
@implementation ClassA
-(id)copyWithZone:(NSZone *)zone
{
    ClassA *A=[[[self class] alloc]init];
    A.x=@"x";
    return  A;
}
@end

类ClassB:

@interface ClassB : ClassA<NSCopying>
@property (nonatomic,copy) NSString *y;
@end
@implementation ClassB
-(id)copyWithZone:(NSZone *)zone
{
    ClassB *B=[super copyWithZone:zone]; //由基类分配空间
    B.y=@"yyy";
    return  B;
}
@end

测试:

    ClassB *b=[ClassB new];
    ClassB *c=[b copyWithZone:nil];
    NSLog(@"%@", c.x);
    NSLog(@"%@", c.y);
    /*
     9-02-24 09:38:35.226065+0800 JJ[5517:627951] x
     2019-02-24 09:38:35.226215+0800 JJ[5517:627951] yyy
     */

7总结:

1、
使用“=”给对象赋值:指针拷贝
2、
向不可变对象发送Copy消息:指针拷贝
向可变对象发送Copy消息:内容拷贝
向不可变对象发送MutableCopy消息:内容拷贝
向可变对象发送MutableCopy消息:内容拷贝

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值