iOS基础学习

Objective-C基础学习之Copy

2017-10-26  本文已影响10人  WenJim

1.copy基本概念

2.Copy的使用

@protocol NSCopying
- (id)copyWithZone:(NSZone *)zone;
@end
@protocol NSMutableCopying
- (id)mutableCopyWithZone:(NSZone *)zone;
@end

3.深复制和浅复制

    NSString *srcStr = @"cwj";
    NSString *copyStr = [srcStr copy];
    NSLog(@"src = %p, copy = %p", srcStr, copyStr);
    NSString *srcStr = @"cwj";
    NSMutableString *copyStr = [srcStr mutableCopy];
    NSLog(@"src = %p, copy = %p", srcStr, copyStr);
    NSLog(@"src = %@, copy = %@", srcStr, copyStr);
    [copyStr appendString:@" love"];
    NSLog(@"src = %@, copy = %@", srcStr, copyStr);
    NSMutableString *srcStr = [NSMutableString stringWithFormat:@"cwj"];
    NSString *copyStr = [srcStr copy];
    [srcStr appendString:@" love"];
    NSLog(@"src = %p, copy = %p", srcStr, copyStr);
    NSLog(@"src = %@, copy = %@", srcStr, copyStr);
    NSMutableString *srcStr = [NSMutableString stringWithFormat:@"cwj"];
    NSMutableString *copyStr = [srcStr mutableCopy];
    [srcStr appendString:@" love"];
    [copyStr appendString:@" abc"];
    NSLog(@"src = %p, copy = %p", srcStr, copyStr);
    NSLog(@"src = %@, copy = %@", srcStr, copyStr);
浅拷贝与深拷贝.png
上一篇下一篇

猜你喜欢

热点阅读