在我們開發(fā)中常常會(huì)遇到這樣的情況,當(dāng)前是列表頁面 剧包,點(diǎn)擊后進(jìn)詳情頁面恐锦,會(huì)把列表頁面的model傳到詳情頁面,如圖:
有時(shí)候可能會(huì)在詳情頁做修改疆液,
這樣就會(huì)影響列表頁面數(shù)據(jù) 一铅。
///思考: 一開始我覺得是 數(shù)組的問題,傳過去的數(shù)組應(yīng)該是需要copy一下堕油,測(cè)試結(jié)果發(fā)現(xiàn)潘飘,數(shù)組指針地址不同,但是里面的model指針確實(shí)相同的掉缺,所以需要把model copy一下卜录,鑒于之前遇到過modelcopy問題,如下是解決問題的方法:
```
//創(chuàng)建數(shù)據(jù)
for (NSInteger index = 0; index< 5; index++) {
MyModel *myModel = [[MyModel alloc]init];
myModel.name = [NSString stringWithFormat:@"我是:%ld",index];
[_modelArr addObject:myModel];
}
//頁面?zhèn)髦?/p>
MineViewController *mine = [MineViewController new];
mine.muArr = _modelArr;
```
//重點(diǎn) model 要 寫copy協(xié)議//model .h 中 簽協(xié)議@interface MyModel : NSObject//model .m 中重寫方法
```
- (id)copyWithZone:(NSZone *)zone{
MyModel * model = [[MyModel allocWithZone:zone] init];
model.name =self.name ;
return model;
}
- (id)mutableCopyWithZone:(NSZone *)zone{
MyModel * model = [[MyModel allocWithZone:zone] init];
model.name =self.name ;
return model;
}
```
//詳情頁面 初始化數(shù)據(jù)
```
NSMutableArray *muArr = [NSMutableArray new];
for (NSInteger index = 0; index < self.muArr.count; index++) {
MyModel *model = [self.muArr[index] copy];
[muArr addObject:model];
}
self.muArr = ?muArr;
```
這樣就不會(huì)影響列表頁面的數(shù)據(jù)了眶明。
優(yōu)化:model屬性過多的時(shí)候 一個(gè)一個(gè)寫比較麻煩艰毒,我這里用到了一個(gè)比較好的方法 。
```
#import@interface PlayModel : NSObject@property(nonatomic,strong)NSNumber *num;
@property(nonatomic,strong)NSDictionary *dic;
@property(nonatomic,assign)BOOL isOpen;
@property(nonatomic,copy)NSString *age;
@property(nonatomic,copy)NSString *name;
@property(nonatomic,copy)NSString *age2;
@property(nonatomic,copy)NSString *name2;
@property(nonatomic,copy)NSString *age3;
@property(nonatomic,copy)NSString *name3;
- (NSArray *) allPropertyNames;
- (id ) displayCurrentModlePropertyBy:(NSString *)propertyName;
@end
```
//.m ?中
```
#import<objc/runtime.h>
- (id)copyWithZone:(NSZone *)zone {
PlayModel *instance = [[PlayModel allocWithZone:zone] init];
NSArray *modelNames = ? ?[self allPropertyNames];
for (NSInteger index = 0; index < modelNames.count; index ++) {
[instance setValue:[self displayCurrentModlePropertyBy:modelNames[index]] forKey:modelNames[index]];
}
return instance;
}
- (id)mutableCopyWithZone:(NSZone *)zone {
PlayModel *instance = [[PlayModel allocWithZone:zone] init];
NSArray *modelNames = ? ?[self allPropertyNames];
for (NSInteger index = 0; index < modelNames.count; index ++) {
[instance setValue:[self displayCurrentModlePropertyBy:modelNames[index]] forKey:modelNames[index]];
}
return instance;
}
///通過運(yùn)行時(shí)獲取當(dāng)前對(duì)象的所有屬性的名稱搜囱,以數(shù)組的形式返回
- (NSArray *) allPropertyNames{
///存儲(chǔ)所有的屬性名稱
NSMutableArray *allNames = [[NSMutableArray alloc] init];
///存儲(chǔ)屬性的個(gè)數(shù)
unsigned int propertyCount = 0;
///通過運(yùn)行時(shí)獲取當(dāng)前類的屬性
objc_property_t ?* propertys = class_copyPropertyList([self class], &propertyCount);
//把屬性放到數(shù)組中
for (int i = 0; i < propertyCount; i ++) {
///取出第一個(gè)屬性
objc_property_t property = propertys[i];
const char * propertyName = property_getName(property);
[allNames addObject:[NSString stringWithUTF8String:propertyName]];
}
///釋放
free(propertys);
return allNames;
}
#pragma mark -- 通過字符串來創(chuàng)建該字符串的Setter方法丑瞧,并返回
- (SEL) creatGetterWithPropertyName: (NSString *) propertyName{
//1.返回get方法: oc中的get方法就是屬性的本身
return NSSelectorFromString(propertyName);
}
//獲取
- (id) displayCurrentModlePropertyBy:(NSString *)propertyName{
//接收返回的值
NSObject *__unsafe_unretained returnValue = nil;
//獲取get方法
SEL getSel = [self creatGetterWithPropertyName:propertyName];
NSLog(@"propertyName : %@",propertyName);
if ([self respondsToSelector:getSel]) {
//獲得類和方法的簽名
NSMethodSignature *signature = [self methodSignatureForSelector:getSel];
//從簽名獲得調(diào)用對(duì)象
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
//設(shè)置target
[invocation setTarget:self];
//設(shè)置selector
[invocation setSelector:getSel];
//調(diào)用
[invocation invoke];
//獲得返回值類型
const char *returnType = signature.methodReturnType;
//如果沒有返回值,也就是消息聲明為void犬辰,那么returnValue=nil
if( !strcmp(returnType, @encode(void)) ){
returnValue = ?nil;
}
//如果返回值為對(duì)象嗦篱,那么為變量賦值
else if( !strcmp(returnType, @encode(id)) ){
[invocation getReturnValue:&returnValue];
}
else{
//如果返回值為普通類型NSInteger ?BOOL
//返回值長度
NSUInteger length = [signature methodReturnLength];
//根據(jù)長度申請(qǐng)內(nèi)存
void *buffer = (void *)malloc(length);
//為變量賦值
[invocation getReturnValue:buffer];
if( !strcmp(returnType, @encode(BOOL)) ) {
returnValue = [NSNumber numberWithBool:*((BOOL*)buffer)];
}
else if( !strcmp(returnType, @encode(NSInteger)) ){
returnValue = [NSNumber numberWithInteger:*((NSInteger*)buffer)];
}else{
returnValue = [NSValue valueWithBytes:buffer objCType:returnType];
}
}
? ? ? //接收返回值
// ? ? ? ?[invocation getReturnValue:&returnValue];
NSLog(@"returnValue ?: %@",returnValue);
}
return ?returnValue ;
}
```
結(jié)語:iOS中model需要重寫copy 方法才能實(shí)現(xiàn)深copy,默認(rèn)的情況下傳的是指針地址幌缝;
一般在列表創(chuàng)建的數(shù)據(jù)源灸促,到詳情頁面需要深copy一下,防止發(fā)在詳情頁面修改了數(shù)據(jù)影響上一層的數(shù)據(jù)涵卵。一位老司機(jī)說過浴栽,每個(gè)頁面從后臺(tái)獲取的數(shù)據(jù)能獲取的盡量獲取,不要從上個(gè)頁面?zhèn)鬟f轿偎。
?五一愉快典鸡。