OC高效率52之理解NSCopying协议
2016-02-27 00:00
411 查看
#import <Foundation/Foundation.h> @interface EOCPerson : NSObject<NSCopying,NSMutableCopying> @property (nonatomic, copy , readonly) NSString *firstName; @property (nonatomic, copy , readonly) NSString *lastName; -(id)initWithFirstName:(NSString *)firstName andLastName:(NSString *)lastName; -(void)addFriend:(EOCPerson *)person; -(void)removeFriend:(EOCPerson *)person; @end #import "EOCPerson.h" @implementation EOCPerson { NSMutableSet *_friend; } -(id)initWithFirstName:(NSString *)firstName andLastName:(NSString *)lastName{ if (self = [super init]){ _friend = [NSMutableSet new]; _lastName = [lastName copy]; _firstName = [firstName copy]; } return self; } -(void)addFriend:(EOCPerson *)person{ [_friend addObject:person]; } -(void)removeFriend:(EOCPerson *)person{ [_friend removeObject:person]; } /** * NSCopying * * @param zone * * @return */ -(id)copyWithZone:(NSZone *)zone{ EOCPerson *person = [[[self class] allocWithZone:zone]initWithFirstName:_firstName andLastName:_lastName]; person->_friend = [_friend mutableCopy]; return person; } /** * NSMutableCopying */ -(id)mutableCopyWithZone:(NSZone *)zone{ return nil; } /** * 深拷贝 */ -(id)deepCopy{ EOCPerson *copy = [[[self class]alloc]initWithFirstName:_firstName andLastName:_lastName]; copy->_friend = [[NSMutableSet alloc]initWithSet:_friend copyItems:YES]; return copy; } @end
![](http://static.oschina.net/uploads/space/2016/0227/214411_nNqG_2319073.jpg)
详谈深浅拷贝:
参考文章链接:http://www.cnblogs.com/langtianya/p/3722129.html
相关文章推荐
- CentOS-6.5系统基础优化附带优化脚本
- centos ab使用
- 记录一些网站
- tomcat部署jfinal
- mac下配置jsp开发环境——tomcat安装及配置
- apache 限制指定user_agent
- 线上Nginx跳转存在的接口返回异常问题
- Opencv 图像叠加 添加水印
- Linux (三) GCC 和 GDB
- Android 4.0 Camera架构分析之preview和takePicture
- 【Linux运维入门】安装sonar报错:JDBCError: Specified key was too long;
- Linux精讲——chown命令
- #分布式系统架构之# 事件驱动模式以及与之匹配的长时间处理过程讨论
- linux的chmod、chown详解
- centos redhat 国内yum源
- 01 Linux档案与目录管理
- Linux (二) vi
- ubuntu卡在stopping restore sound card state
- Linux精讲——chmod命令
- HBase的Shell操作