copy and copyWithZone

定義一個Person類

#import <Foundation/Foundation.h>

@interface Person : NSObject

@property (nonatomic, strong) NSString *strongName;

@property (nonatomic, copy) NSString *coName;

@end

然后在VC中調(diào)用Person

- (void)testPerson {
    NSMutableString *someName = [NSMutableString stringWithString:@"Chris"];
    
    Person *p = [[Person alloc] init];
    p.strongName = someName;
    p.coName = someName;
    
    [someName setString:@"Debajit"];
//    The current value of the Person.name property will be different depending on whether the property is declared retain or copy — it will be @"Debajit" if the property is marked retain, but @"Chris" if the property is marked copy.
//    Since in almost all cases you want to prevent mutating an object's attributes behind its back, you should mark the properties representing them copy. (And if you write the setter yourself instead of using @synthesize you should remember to actually use copy instead of retain in it.)
    NSLog(@"strongName: %@",p.strongName);
    NSLog(@"copyName : %@",p.coName);
}
輸出:
//     strongName: Debajit
//     copyName : Chris

使用strong修飾的name字符串在賦值之后,如果字符串改變,會影響之前name的內(nèi)容。
使用copy修飾的字符串在值修改之后則不會受到影響。

一個類要使用copy方法需要實(shí)現(xiàn)copyWithZone的協(xié)議
定義一個UserInfo類

#import <Foundation/Foundation.h>

@interface UserInfo : NSObject <NSCopying>

@property (nonatomic, copy) NSString *firstName;
@property (nonatomic, copy) NSString *lastName;

- (instancetype)initWithFirstName:(NSString *)firstName lastName:(NSString *)lastName;

@end
#import "UserInfo.h"

@implementation UserInfo

- (instancetype)initWithFirstName:(NSString *)firstName lastName:(NSString *)lastName {
    
    if (self = [super init]) {
        self.firstName = [firstName copy];
        self.lastName = [lastName copy];
    }
    
    return self;
}

- (id)copyWithZone:(NSZone *)zone {
    
    UserInfo *info = [[[self class] allocWithZone:zone] initWithFirstName:self.firstName lastName:self.lastName];
    return info;
}


@end

實(shí)現(xiàn)copyWithZone的協(xié)議方法。
在vc中調(diào)用UserInfo

- (void)testUserInfo {
    
    
    UserInfo *obj1 = [[UserInfo alloc] initWithFirstName:@"zhang" lastName:@"san"];
    
    NSLog(@"obj1 address : %p  %@,%@",obj1,obj1.firstName,obj1.lastName);
    
    UserInfo *copyObj1 = [obj1 copy]; //復(fù)制一個新對象,到一個新的內(nèi)存地址,并把值一并復(fù)制過去。
    
    NSLog(@"copyObj1 address : %p  %@,%@",copyObj1,copyObj1.firstName,copyObj1.lastName);
    
    copyObj1.firstName = @"li";
    copyObj1.lastName = @"si";
    NSLog(@"obj1 address : %p  %@,%@",obj1,obj1.firstName,obj1.lastName);
    NSLog(@"copyObj1 address : %p  %@,%@",copyObj1,copyObj1.firstName,copyObj1.lastName);
  
輸出:
 obj1 address : 0x60400002e8c0  zhang,san
 copyObj1 address : 0x60800002fe80  zhang,san
 obj1 address : 0x60400002e8c0  zhang,san
 copyObj1 address : 0x60800002fe80  li,si
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容