美文网首页
copyWithZone: 的正确写法

copyWithZone: 的正确写法

作者: Source_Chang | 来源:发表于2020-10-19 14:57 被阅读0次
@interface Teacher : NSObject

@end

@implementation Teacher

@end

@interface Student : NSObject <NSCopying>

@property (nonatomic, strong, nullable) Teacher *teacher;

@end

@implementation Student


- (instancetype)copyWithZone:(NSZone *)zone {
    
    Student *student = [[[self class] allocWithZone:zone] init];
    if ( [self.teacher conformsToProtocol:@protocol(NSCopying)] ) {
        
        student.teacher = [self.teacher copy];
        
    } else {
        
        student.teacher = self.teacher;
    }
    
    return student;
}

相关文章

网友评论

      本文标题:copyWithZone: 的正确写法

      本文链接:https://www.haomeiwen.com/subject/pwywpktx.html