IOS学习笔记十八(copy、mutableCopy、NSCopying、NSMutableCopy、深复制、浅复制)

举报
chenyu 发表于 2021/07/27 01:44:39 2021/07/27
【摘要】 1、 copy、mutableCopy方法 copy方法返回对象的不可修改的副本 mutableCopy方法返回的对象可修改的副本   1)、测试demo int main(int argc, char * argv[]) { @autoreleasepool { NSMutableString *book = [NSMutableString strin...

1、 copy、mutableCopy方法

copy方法返回对象的不可修改的副本

mutableCopy方法返回的对象可修改的副本

 

1)、测试demo


  
  1. int main(int argc, char * argv[]) {
  2. @autoreleasepool {
  3. NSMutableString *book = [NSMutableString stringWithString:@"chenyu"];
  4. NSMutableString *bookCopy = [book mutableCopy];
  5. [bookCopy replaceCharactersInRange:NSMakeRange(1, 2) withString:@"gong"];
  6. NSLog(@"book is %@", book);
  7. NSLog(@"bookCopy is %@", bookCopy);
  8. NSString *str = @"chenyu";
  9. NSMutableString *strCopy = [str mutableCopy];
  10. [strCopy appendString:@"chenyu"];
  11. NSLog(@"strCopy is:%@", strCopy);
  12. //由于str2是不可变的,所以运行下面会报错
  13. NSMutableString *str2 = [str copy];
  14. NSLog(@"str copy chen is:%@", str2);
  15. // [str2 appendString:@"chenyu"];
  16. }
  17. }

 

2)、运行结果

 


  
  1. 2018-07-15 19:03:35.564049+0800 cyTest[27254:9418105] book is chenyu
  2. 2018-07-15 19:03:35.565157+0800 cyTest[27254:9418105] bookCopy is cgongnyu
  3. 2018-07-15 19:03:35.566056+0800 cyTest[27254:9418105] strCopy is:chenyuchenyu
  4. 2018-07-15 19:03:35.566857+0800 cyTest[27254:9418105] str copy chen is:chenyu

 

 

 

 

 

 

 

2、NSCopying、NSMutableCopy协议

对象调用copy方法来复制自身的可变副本,需要类 实现NSCopying协议,让该类实现copyWithZone方法

对象调用mutableCopy方法来复制自身的可变副本,需要类实现NSMutbaleCopying协议,让类实现mutableCopyWithZone方法demo可以看下下面写的浅复制

 

 

 

 

 

 

 

 

3、深复制和浅复制

我个人理解感觉浅复制是复制后的属性(指针变量)指向的地址都是同一块地址,当复制后的对象属性值发生变化的时候,原始值也发生了变化。

深复制就是复制后的属性(指针变量)指向的地址不是同一块地址,当复制后的对象属性值发生变化的时候,原始值不会发生变化。

 

1)、浅复制测试Demo

Dog.h


  
  1. #import <Foundation/Foundation.h>
  2. #ifndef Dog_h
  3. #define Dog_h
  4. @interface Dog : NSObject<NSCopying>
  5. @property (nonatomic, strong) NSMutableString *name;
  6. @property (nonatomic, assign) int age;
  7. @end
  8. #endif /* Dog_h */

 

Dog.m

 


  
  1. #import <Foundation/Foundation.h>
  2. #import "Dog.h"
  3. @implementation Dog
  4. @synthesize name;
  5. @synthesize age;
  6. -(id)copyWithZone:(NSZone *)zone
  7. {
  8. Dog *dog = [[[self class] allocWithZone:zone] init];
  9. dog.name = self.name;
  10. dog.age = self.age;
  11. return dog;
  12. }
  13. @end

 

main.m

 


  
  1. Dog *dog1 = [Dog new];
  2. dog1.name = [NSMutableString stringWithString:@"chen"];
  3. dog1.age = 1;
  4. NSLog(@"dog1.name is %@ and age is %d", dog1.name, dog1.age);
  5. //浅复制
  6. Dog *dog2 = [dog1 copy];
  7. // dog2.name = [NSMutableString stringWithString:@"li"];
  8. [dog2.name replaceCharactersInRange:NSMakeRange(1, 2) withString:@"hel"];
  9. dog2.age = 20;
  10. NSLog(@"dog2.name is %@ and age is %d", dog2.name, dog2.age);
  11. NSLog(@"dog1.name is %@ and age is %d", dog1.name, dog1.age);

 

2)、运行结果

 


  
  1. 2018-07-15 19:03:35.567556+0800 cyTest[27254:9418105] dog1.name is chen and age is 1
  2. 2018-07-15 19:03:35.567690+0800 cyTest[27254:9418105] dog2.name is cheln and age is 20
  3. 2018-07-15 19:03:35.567768+0800 cyTest[27254:9418105] dog1.name is cheln and age is 1

 

3)、深复制Demo

 

Dog.h文件一样

Dog.m文件有点变化


  
  1. #import <Foundation/Foundation.h>
  2. #import "Dog.h"
  3. @implementation Dog
  4. @synthesize name;
  5. @synthesize age;
  6. -(id)copyWithZone:(NSZone *)zone
  7. {
  8. Dog *dog = [[[self class] allocWithZone:zone] init];
  9. // dog.name = self.name;
  10. dog.name = [self.name mutableCopy];
  11. dog.age = self.age;
  12. return dog;
  13. }
  14. @end

 

main.m文件一样

 


  
  1. Dog *dog1 = [Dog new];
  2. dog1.name = [NSMutableString stringWithString:@"chen"];
  3. dog1.age = 1;
  4. NSLog(@"dog1.name is %@ and age is %d", dog1.name, dog1.age);
  5. //深复制
  6. Dog *dog2 = [dog1 copy];
  7. // dog2.name = [NSMutableString stringWithString:@"li"];
  8. [dog2.name replaceCharactersInRange:NSMakeRange(1, 2) withString:@"hel"];
  9. dog2.age = 20;
  10. NSLog(@"dog2.name is %@ and age is %d", dog2.name, dog2.age);
  11. NSLog(@"dog1.name is %@ and age is %d", dog1.name, dog1.age);

 

4)、运行结果

 


  
  1. 2018-07-15 19:13:08.751335+0800 cyTest[27529:9427019] dog1.name is chen and age is 1
  2. 2018-07-15 19:13:08.751686+0800 cyTest[27529:9427019] dog2.name is cheln and age is 20
  3. 2018-07-15 19:13:08.751885+0800 cyTest[27529:9427019] dog1.name is chen and age is 1

 

 

 

 

 

 

 

4、setter方法的复制选项

我们setter方法复制选项的时候,如果加了copy属性,比如setName方法


  
  1. -(void)setName : (NSMutableStrinfg *)name
  2. {
  3. self.name = [name copy];
  4. }

  
  1. #import <Foundation/Foundation.h>
  2. #import "Dog.h"
  3. @implementation Dog
  4. @synthesize name;
  5. @synthesize age;
  6. @end

 

所以当对象设置了name属性的话,就不能再改变了,如下demo

 

Dog.h


  
  1. #import <Foundation/Foundation.h>
  2. #ifndef Dog_h
  3. #define Dog_h
  4. @interface Dog : NSObject
  5. @property (nonatomic, copy) NSMutableString *name;
  6. @property (nonatomic, assign) int age;
  7. @end
  8. #endif /* Dog_h */

 

Dog.m

 


  
  1. #import <Foundation/Foundation.h>
  2. #import "Dog.h"
  3. @implementation Dog
  4. @synthesize name;
  5. @synthesize age;
  6. @end

 

main.m

 


  
  1. Dog *dog1 = [Dog new];
  2. dog1.name = [NSMutableString stringWithString:@"chen"];
  3. dog1.age = 1;
  4. [dog1.name appendString:@"hello"];

 

运行结果


  
  1. 2018-07-15 19:22:17.736557+0800 cyTest[27853:9436238] -[NSTaggedPointerString appendString:]: unrecognized selector sent to instance 0xa0000006e6568634
  2. 2018-07-15 19:22:17.739655+0800 cyTest[27853:9436238] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSTaggedPointerString appendString:]: unrecognized selector sent to instance 0xa0000006e6568634'
  3. *** First throw call stack:
  4. (
  5. 0 CoreFoundation 0x0000000104fd61e6 __exceptionPreprocess + 294
  6. 1 libobjc.A.dylib 0x000000010466b031 objc_exception_throw + 48
  7. 2 CoreFoundation 0x0000000105057784 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
  8. 3 CoreFoundation 0x0000000104f58898 ___forwarding___ + 1432
  9. 4 CoreFoundation 0x0000000104f58278 _CF_forwarding_prep_0 + 120
  10. 5 cyTest 0x0000000103d60438 main + 600
  11. 6 libdyld.dylib 0x0000000108a2f955 start + 1
  12. )
  13. libc++abi.dylib: terminating with uncaught exception of type NSException
  14. (lldb)

很明显,报错了。

 

文章来源: chenyu.blog.csdn.net,作者:chen.yu,版权归原作者所有,如需转载,请联系作者。

原文链接:chenyu.blog.csdn.net/article/details/81055781

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。