copy and mutableCopy

Posted step by step,go along

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了copy and mutableCopy相关的知识,希望对你有一定的参考价值。

结论:

 

 

 

1, 深复制与浅复制

 

 

 

 2,immutable和mutable

 

 

3,代码分析:
 

#pragma mark - String

- (void)stringCopyAndMutableCopy {
    NSString *originStr = @"123";
    NSString *copyStr = [originStr copy];//浅复制
    NSString *mutableCopyStr = [originStr mutableCopy];//深复制
    NSLog(@"string origin = %p, copy = %p, mutableCopy = %p", originStr, copyStr, mutableCopyStr);
    NSLog(@"str copy class = %s, mutableCopy class = %s", object_getClassName(copyStr), object_getClassName(mutableCopyStr));
}

- (void)mutableStringCopyAndMutableCopy {
    NSMutableString *originMutableStr = [NSMutableString stringWithString:@"123"];
    NSString *copyMutableStr = [originMutableStr copy];// 深复制
    NSString *mutableCopyMutableStr = [originMutableStr mutableCopy];//深复制
    NSLog(@"mutableStr origin = %p, copy = %p, mutableCopy = %p", originMutableStr, copyMutableStr, mutableCopyMutableStr);
    NSLog(@"mutableStr copy class = %s, mutableCopy class = %s", object_getClassName(copyMutableStr), object_getClassName(mutableCopyMutableStr));
}

#pragma mark - Array

/**
 *
 *  __NSArrayM->NSMutableArray->NSArray
 *  __NSArrayI->NSArray
 */


- (void)arrayCopyAndMutableCopy {
    NSArray *orginArray = @[@"123"];
    NSArray *copyArray = [orginArray copy];
    NSArray *mutableCopyArray = [orginArray mutableCopy];
    NSLog(@"array origin = %p, copy = %p, mutableCopy = %p", orginArray, copyArray, mutableCopyArray);
    NSLog(@"array copy class = %s, mutableCopy class = %s", object_getClassName(copyArray), object_getClassName(mutableCopyArray));
}

- (void)mutableArrayCopyAndMutableCopy {
    NSMutableArray *orginMutableArray = [NSMutableArray arrayWithObject:@"123"];
    NSArray *copyMutableArray = [orginMutableArray copy];
    NSArray *mutableCopyMutableArray = [orginMutableArray mutableCopy];
    NSLog(@"mutableArray origin = %p, copy = %p, mutableCopy = %p", orginMutableArray, copyMutableArray, mutableCopyMutableArray);
    NSLog(@"mutableArray copy class = %s, mutableCopy class = %s", object_getClassName(copyMutableArray), object_getClassName(mutableCopyMutableArray));
}

 

示例代码:https://github.com/shilihuan/CopyAndMutableCopy

 

以上是关于copy and mutableCopy的主要内容,如果未能解决你的问题,请参考以下文章

copy与mutableCopy

copy mutableCopy

Copy && MutableCopy

copy 与 retain的区别,以及copy与mutablecopy的区别

Objective-C中mutableCopy和copy的理解

Foundation框架 之 NSFileManager 与 copy & mutableCopy