iOS 对象的copy
页面面通过对象进行传值 例如 A传对象m到B 在B 修改时不影响 A的值用到
步骤
1、在Class @interface上声明NSCopying协议,
2、在Class @implementation中实现协议方法- (id)copyWithZone:(NSZone *)zone。
使用runtime,避免一个一个属性的去赋值
#import <objc/runtime.h>
-(id)copyWithZone:(NSZone *)zone {
CourseModel *model = [[self class] allocWithZone:zone];
unsigned int count = 0;
objc_property_t *properties = class_copyPropertyList([CourseModel class], &count);
for (int i = 0; i < count; i++) {
const char *name = property_getName(properties[i]);
NSString *propertyName = [NSString stringWithUTF8String:name];
id propertyValue = [self valueForKey:propertyName];
if (propertyValue) {
[model setValue:propertyValue forKey:propertyName];
}
}
free(properties);
return model;
}