我们经常会遇到这一种情况:在界面push的时候会将这个界面的值传到下个界面,一般情况下使用vc.model = self.model没任何问题,但是有一种情况下就是当传值得时候会改变vc的一些属性的值,但是这个时候 ,由于self.model与vc.model指向同一个对象,会让self.model跟着改变,那当我们pop会这个界面时 ,想在做一些操作的时候,发现与预期不符,那么怎么解决这个问题呢?
这个时候我们就需要重新生成一个对象了,使用copy来实现,如果我们使用的是foundation框架的对象时候是直接可以的(比如NSArray,NSDictionary),因为所有类都已经实现了NSCopying协议,当我们使用自己创建的类的时候,就需要自己来实现该协议了。具体怎么做呢?
实现NSCopying协议
@interface RecipeDetailModel:BaseModel<NSCopying>
实现协议方法
//实现NSCopy协议,使对象能够被copy -(id)copyWithZone:(NSZone *)zone{ RecipeDetailModel *model = [[[self class] allocWithZone:zone]init]; return model; }
当然因为是新开辟的对象 需要我们手动赋值,这个时候问题来了,如果属性过多,会这样:
@property (nonatomic,assign) NSInteger copyNum; @property (nonatomic,assign) NSInteger giveFlag; @property (nonatomic,copy) NSString *recipeCode; @property (nonatomic,copy) NSString *payModeText; @property (nonatomic,assign) NSInteger recipeId; @property (nonatomic,assign) NSInteger chooseFlag; @property (nonatomic,copy) NSString *tcmUsePathways; @property (nonatomic,copy) NSString *checkerText; @property (nonatomic, copy) NSString *checkDate; @property (nonatomic,copy)NSString *checkerTel; @property (nonatomic,assign) NSInteger clinicOrgan; @property (nonatomic,copy) NSString *fromflagText; @property (nonatomic,copy) NSString *mpiid; @property (nonatomic,copy) NSString *checkOrganText; @property (nonatomic,copy) NSString *lastModify; @property (nonatomic,copy) NSString *giveModeText; @property (nonatomic,assign) CGFloat actualPrice; @property (nonatomic,copy) NSString *signDate; @property (nonatomic,copy) NSString *address3Text; @property (nonatomic,assign) NSInteger giveMode; @property (nonatomic,copy) NSString *recipeSurplusHours; @property (nonatomic,assign) NSInteger medicalPayFlag; @property (nonatomic,copy) NSString *address2Text; @property (nonatomic,copy) NSString *createDate; @property (nonatomic,assign) NSInteger depart; @property (nonatomic,assign) CGFloat totalMoney; @property (nonatomic,assign) CGFloat price1; @property (nonatomic,assign) CGFloat price2; @property (nonatomic,assign) NSInteger signFile; @property (nonatomic,assign) NSInteger pushFlag; @property (nonatomic,assign) NSInteger remindFlag; @property (nonatomic,copy) NSString *departText; @property (nonatomic,assign) NSInteger valueDays; @property (nonatomic,copy) NSString *address1Text; @property (nonatomic,assign) EpMedicineType recipeType; @property (nonatomic,copy) NSString *recipeTypeText; @property (nonatomic,copy) NSString *doctorText; @property (nonatomic,assign) NSInteger fromflag; @property (nonatomic,assign) NSInteger doctor; @property (nonatomic,copy) NSString *organDiseaseId; @property (nonatomic,copy) NSString *organDiseaseName; @property (nonatomic,copy) NSString *tcmUsingRate; @property (nonatomic,copy) NSString *clinicOrganText; @property (nonatomic,copy) NSString *memo; @property (nonatomic,assign) RecipeStatus status; @property (nonatomic,copy) NSString *statusText; @property (nonatomic,assign) NSInteger payFlag; @property (nonatomic,copy) NSString *recipeMemo; //处方 医嘱 @property (nonatomic,copy) NSString *checkFailMemo;//过检失败原因 @property (nonatomic,strong) NSMutableArray <RecipeReject*> *rejectArray;//审方拒绝原因
,每个属性就得像model.copyNum = self.copyNum这样进行赋值,当然这样写也没问题,但是作为一个以写简洁代码为目标的我(和很多程序员)忍不了,所以我开始思考有没有其他方式
- 结果想到的就是利用runtime来实现,通过class_copyPropertyList来获取该类所有属性,然后for循环来赋值:
//实现NSCopy协议,使对象能够被copy
-(id)copyWithZone:(NSZone *)zone{
RecipeDetailModel *model = [[[self class] allocWithZone:zone]init];
unsigned int outCount;
objc_property_t *properties = class_copyPropertyList([self class], &outCount);
for (int i = 0; i < outCount; i ++ ) {
const char * name = property_getName(properties[i]);
NSString *nameStr = [NSString stringWithFormat:@"%s",name];
//KVC
[model setValue:[self valueForKey:nameStr] forKey:nameStr];
}
return model;
}
,for循环中先获取属性名称,利用kvc来为对象赋值,因为model对象与self对象有相同属性,所以可以完全赋值