自定义对象|自定义对象 实现 Copy 和 MutableCopy
【自定义对象|自定义对象 实现 Copy 和 MutableCopy】如果是我们定义的对象,那么我们自己要实现NSCopying
,NSMutableCopying
这样就能调用copy
和mutablecopy
了。
举个例子:
@interface MyObj : NSObject
{
NSMutableString *name;
NSString *imutableStr;
int age;
}
@property (nonatomic, retain) NSMutableString *name;
@property (nonatomic, retain) NSString *imutableStr;
@property (nonatomic) int age;
@end
@implementation MyObj
@synthesize name;
@synthesize age;
@synthesize imutableStr;
- (id)init
{
if (self = [super init])
{
self.name = [[NSMutableString alloc]init];
self.imutableStr = [[NSString alloc]init];
age = -1;
}
return self;
}
- (void)dealloc
{
[name release];
[imutableStr release];
[super dealloc];
}
- (id)copyWithZone:(NSZone *)zone
{
MyObj *copy = [[[self class] allocWithZone:zone] init];
copy.name = [name copy];
copy.imutableStr = [imutableStr copy];
//copy->name = [name copyWithZone:zone];
;
//copy->imutableStr = [name copyWithZone:zone];
//
copy->age = age;
return copy;
}
- (id)mutableCopyWithZone:(NSZone *)zone
{
MutableCopyObject*mutableCopy = [[[self class] alloc] init];
copy.name = [self.name mutableCopy];
copy.age = [imutableStr mutableCopy];
return copy;
}
推荐阅读
- 关于QueryWrapper|关于QueryWrapper,实现MybatisPlus多表关联查询方式
- MybatisPlus使用queryWrapper如何实现复杂查询
- python学习之|python学习之 实现QQ自动发送消息
- 孩子不是实现父母欲望的工具——林哈夫
- SpringBoot调用公共模块的自定义注解失效的解决
- opencv|opencv C++模板匹配的简单实现
- Node.js中readline模块实现终端输入
- python自定义封装带颜色的logging模块
- java中如何实现重建二叉树
- 列出所有自定义的function和view