Objective-C Copy语法(二)对于我们自定义对象Copy以及[self class]相关的错误
2014-03-12 16:15
561 查看
@对于自定义对象的Copy:该类必须实现 NSCopying 协议,重写 copyWithZone 方法.
@对于自定义对象的mutableCopy:必须实现 NSMutableCopying协议,重写 utableCopyWithZone 方法。
看看下面的一个demo:
@interface Student : NSObject <NSCopying>
@property (nonatomic, copy) NSString *name;
+ (id)studentWithName:(NSString *)name;
@end
@implementation Student
+ (id)studentWithName:(NSString *)name {
// 注:[self class]返回的是类,打印出来就相当于类名
// 这里[Student alloc]最好写成[self class]
Student * student = [[[[self class] alloc] init] autorelease];
student.name = name;
return student;
}
#pragma mark copying协议的方法 ------ 实现对象可变
- (id)copyWithZone:(NSZone *)zone {
// (NSZone *)zone的含义就是为要建副本对象已经准备好了新开辟的内存空间
// 所有copy出来的对象是一个新的对象,修改它的属性不会影响源对象
// 这里[Student alloc]最好写成[self class]
// 注:[self class]返回的是类,打印出来就相当于类名
Student * copy = [[[self class] allocWithZone:zone] init];
// 拷贝名字给副本对象
copy.name = self.name;
return copy;
}
#pragma mark copying协议的方法 ------ 实现对象不可变
- (id)copyWithZone:(NSZone *)zone {
return self;
}
@end
main函数:
#pragma mark 演示Student的copy
void studentCopy() {
Student * student1 = [Student studentWithName:@"student1"];
Student * student2 = [student1 copy];
student2.name = @"student2";
NSLog(@"student1:%@",
student1);
NSLog(@"student2:%@",
student2);
}
@ 为什么我一再强调要写成[self class],下面我们通过让一个子类继承Student,来说明这个问题,顺便也来实现一下子类 继承父类后,如何实现copy(如果某一个子类继承了实现了NSCopying协议的基类,那么该子类也是会自动继承这
个协议的方法,但是需要自己重新实现。)
@interface StupidStudent : Student
@property (nonatomic, assign) int age;
+ (id)stupidStudentWithAge:(int)age name:(NSString *)name;
+ (id)study;
@end
@implementation StupidStudent
+ (id)stupidStudentWithAge:(int)age name:(NSString *)name {
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
// 如果父类Student的初始化那里写成[Student alloc],子类 StupidStudent创建对象利用super指示器去到父类中调用 父类的studentWithName的方法,创建的会是一个父类Student对象,而并不是StupidStudent对象,父类中并不存在
age属性和+ (id)study方法,你拿父类的实例对象去调用不存在的属性和方法,程序就直接报错了
StupidStudent * stupid = [super studentWithName:name];
stupid.age = age;
return stupid;
}
- (id)copyWithZone:(NSZone *)zone {
// 一定要调用父类的方法
GoodStudent * copy = [super copyWithZone:zone];
copy.age = self.age;
return copy;
}
@end
main函数:
void goodStudentCopy() {
StupidStudent * student1 = [StupidStudent stupidStudentWithAge:10 name:@"stupid1"];
StupidStudent * student2 = [student1 copy];
student2.name = @"stupid2";
student2.age = 11;
NSLog(@"student1:%@", student1);
NSLog(@"student2:%@", student2);
}
@对于自定义对象的mutableCopy:必须实现 NSMutableCopying协议,重写 utableCopyWithZone 方法。
看看下面的一个demo:
@interface Student : NSObject <NSCopying>
@property (nonatomic, copy) NSString *name;
+ (id)studentWithName:(NSString *)name;
@end
@implementation Student
+ (id)studentWithName:(NSString *)name {
// 注:[self class]返回的是类,打印出来就相当于类名
// 这里[Student alloc]最好写成[self class]
Student * student = [[[[self class] alloc] init] autorelease];
student.name = name;
return student;
}
#pragma mark copying协议的方法 ------ 实现对象可变
- (id)copyWithZone:(NSZone *)zone {
// (NSZone *)zone的含义就是为要建副本对象已经准备好了新开辟的内存空间
// 所有copy出来的对象是一个新的对象,修改它的属性不会影响源对象
// 这里[Student alloc]最好写成[self class]
// 注:[self class]返回的是类,打印出来就相当于类名
Student * copy = [[[self class] allocWithZone:zone] init];
// 拷贝名字给副本对象
copy.name = self.name;
return copy;
}
#pragma mark copying协议的方法 ------ 实现对象不可变
- (id)copyWithZone:(NSZone *)zone {
return self;
}
@end
main函数:
#pragma mark 演示Student的copy
void studentCopy() {
Student * student1 = [Student studentWithName:@"student1"];
Student * student2 = [student1 copy];
student2.name = @"student2";
NSLog(@"student1:%@",
student1);
NSLog(@"student2:%@",
student2);
}
@ 为什么我一再强调要写成[self class],下面我们通过让一个子类继承Student,来说明这个问题,顺便也来实现一下子类 继承父类后,如何实现copy(如果某一个子类继承了实现了NSCopying协议的基类,那么该子类也是会自动继承这
个协议的方法,但是需要自己重新实现。)
@interface StupidStudent : Student
@property (nonatomic, assign) int age;
+ (id)stupidStudentWithAge:(int)age name:(NSString *)name;
+ (id)study;
@end
@implementation StupidStudent
+ (id)stupidStudentWithAge:(int)age name:(NSString *)name {
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
// 如果父类Student的初始化那里写成[Student alloc],子类 StupidStudent创建对象利用super指示器去到父类中调用 父类的studentWithName的方法,创建的会是一个父类Student对象,而并不是StupidStudent对象,父类中并不存在
age属性和+ (id)study方法,你拿父类的实例对象去调用不存在的属性和方法,程序就直接报错了
StupidStudent * stupid = [super studentWithName:name];
stupid.age = age;
return stupid;
}
- (id)copyWithZone:(NSZone *)zone {
// 一定要调用父类的方法
GoodStudent * copy = [super copyWithZone:zone];
copy.age = self.age;
return copy;
}
@end
main函数:
void goodStudentCopy() {
StupidStudent * student1 = [StupidStudent stupidStudentWithAge:10 name:@"stupid1"];
StupidStudent * student2 = [student1 copy];
student2.name = @"stupid2";
student2.age = 11;
NSLog(@"student1:%@", student1);
NSLog(@"student2:%@", student2);
}
相关文章推荐
- Objective-C Copy语法(一)对于Objective-C 提供的类对象NSString/NSMutableString; NSArray/NSMutableArray...
- Objective-C之语法copy、mutablecopy以及自定义复制功能
- JavaScript内置对象 以及和 内置对象相关的语法
- Sql2012如何将远程服务器数据库及表、表结构、表数据导入本地数据库 自定义日志记录功能,按日记录,很方便 C#常量和字段以及各种方法的语法总结 类型,对象,线程栈,托管堆在运行时的关系,以及clr如何调用静态方法,实例方法,和虚方法 asp.net webapi 自定义身份验证
- grails Domian对象转JSON去class以及自定义字段的最佳方式
- 自定义adapter的基础上Listview优化方案以及几个小错误(checkbox吃掉点击事件以及对象重复问题)
- javascript中的__proto__和prototype,以及ES6中class的原型相关
- Objective-C语法之数组对象的那些事儿(七)
- Objective-C 2.0 with Cocoa Foundation--- 7,对象的初始化以及实例变量的作用域
- Objective-C 2.0 with Cocoa Foundation--- 7,对象的初始化以及实例变量的作用域
- Objective-C语法之集合对象的那些事儿(九)
- android lint check的学习和自定义以及lint语法
- git 上传项目方法以及遇到的相关错误
- Objective-C语法之动态类型(isKindOfClass, isMemberOfClass,id)等NSClassFromString
- .NET/C#中对对象集合进行查询的方法 以及相关的 Predicate<T> 及 Action<T> 的用法
- golang语法学习(二):控制语句,函数,以及错误处理。
- C++初始化列表问题,类中有一个对象类型的数组成员变量,在初始化列表中初始化时报错“[]”操作符语法错误
- Vue.js绑定HTML class数组语法错误(官网错误,同学可以上github上改一下)
- 【XCode7+iOS9】http网路连接请求、MKPinAnnotationView自定义图片和BitCode相关错误
- 黑马程序员-面向对象的理解以及Objective-C方法和对象分配内存的学习笔记