利用Runtime重写copyWithZone

2018-03-05  本文已影响0人  huisedediao
- (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];
    }
    return copyObject;
}
上一篇下一篇

猜你喜欢

热点阅读