实现<NSCopying>协议

作者:追风剑情 发布于:2019-3-7 13:35 分类:Objective-C

示例

#import <Foundation/Foundation.h>

// 实现不可变副本、可变副本协议
@interface ClassA : NSObject <NSCopying, NSMutableCopying>
@property int age;
// 注意:这里使用了copy特性
@property (nonatomic, copy) NSString *theName;

-(id) copyWithZone:(NSZone *)zone;
-(id) mutableCopyWithZone:(NSZone *)zone;
@end

@implementation ClassA
@synthesize age;
@synthesize theName;
-(id) copyWithZone:(NSZone *)zone
{
    NSLog(@"ClassA copyWithZone");
    // 在指定存储区(zone)中分配内存
    //ClassA *newClassA = [[ClassA allocWithZone: zone] init];
    
    // 写成[self class]方便子类调用
    ClassA *newClassA = [[[self class] allocWithZone: zone] init];
    newClassA.age = age;
    // 因为用了copy特性,setter会生成副本[theName copy];
    newClassA.theName = theName;
    return newClassA;
}

-(id) mutableCopyWithZone:(NSZone *)zone
{
    NSLog(@"ClassA mutableCopyWithZone");
    // 在指定存储区(zone)中分配内存
    //ClassA *newClassA = [[ClassA allocWithZone: zone] init];
    
    // 写成[self class]方便子类调用
    ClassA *newClassA = [[[self class] allocWithZone: zone] init];
    newClassA.age = age;
    // 因为用了copy特性,setter会生成副本[theName copy];
    newClassA.theName = theName;
    return newClassA;
}
@end

@interface ClassB : ClassA

@end

@implementation ClassB
-(id) copyWithZone:(NSZone *)zone
{
    NSLog(@"ClassB copyWithZone");
    // 在指定存储区(zone)中分配内存
    // [self class] 确保分配的是新的ClassB对象,而不是ClassA对象
    //ClassB *newClassB = [[[self class] allocWithZone: zone] init];
    //newClassB.age = self.age;
    
    // 考虑到父类可能已经实现了数据复制
    ClassB *newClassB = (ClassB *)[super copyWithZone: zone];
    return newClassB;
}

-(id) mutableCopyWithZone:(NSZone *)zone
{
    NSLog(@"ClassB mutableCopyWithZone");
    // 在指定存储区(zone)中分配内存
    //ClassB *newClassB = [[[self class] allocWithZone: zone] init];
    //newClassB.age = self.age;
    
    // 考虑到父类可能已经实现了数据复制
    ClassB *newClassB = (ClassB *)[super copyWithZone: zone];
    // TODO:: 其他设值
    return newClassB;
}
@end

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        ClassA *classA = [[ClassA alloc] init];
        classA.age = 13;
        classA.theName = @"nameA";
        
        ClassA *copyClassA = [classA copy];
        NSLog(@"copy class A: age=%i, theName=%@", copyClassA.age, copyClassA.theName);
        
        copyClassA.age = 14;
        
        ClassA *mcopyClassA = [classA mutableCopy];
        NSLog(@"mutable copy class A: age=%i, theName=%@", mcopyClassA.age, mcopyClassA.theName);
        
        ClassB *classB = [[ClassB alloc] init];
        classB.age = 15;
        classB.theName = @"nameB";
        
        ClassB *copyClassB = [classB copy];
        NSLog(@"copy class B: age=%i, theName=%@", copyClassB.age, copyClassB.theName);
        
        ClassB *mcopyClassB = [classB mutableCopy];
        NSLog(@"mutable class B: age=%i, theName=%@", mcopyClassB.age, mcopyClassB.theName);
        
        mcopyClassB.theName = @"changeNameB";
        
        NSLog(@"classB.theName=%@", classB.theName);
    }
    return 0;
}

运行测试
111.png

标签: Objective-C

Powered by emlog  蜀ICP备18021003号   sitemap

川公网安备 51019002001593号