首页 > 代码库 > copy and mutableCopy
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。