iOS 利用Runtime重写copyWithZone

- (id)copyWithZone:(NSZone *)zone
{
    id copyObject = [[[self class] allocWithZone:zone] init];
    
    unsigned int count = 0;
    Ivar *ivars = class_copyIvarList([self class], &count);
    for (int i = 0; i < count; i++)
    {
        NSString *key = [[NSString stringWithUTF8String:ivar_getName(ivars[i])] substringFromIndex:1];
        [copyObject setValue:[self valueForKey:key] forKey:key];
    }
    free(ivars);
    return copyObject;
}

你可能感兴趣的:(iOS 利用Runtime重写copyWithZone)