如何将KVO应用于在运行时创建的@dynamic属性?

时间:2016-04-25 06:03:07

标签: ios objective-c key-value-observing dynamic-properties

我正在尽力解释这是如何运作的,但它非常令人困惑和冗长。让我知道我能做些什么来澄清。

我被困在KVC和KVO概念中 我有一个入门级(NSObject) Entry Class具有私有变量objectProperties(NSDictionary),其值来自服务器。

假设objectProperties具有键“price”,“discount”等,那么我想在Entry类的实例中创建动态属性。

这些键可能因响应而异,也可能因动态变量而异。

现在,当用户想要在相同属性上使用KVO时,无法访问它们。

用于创建动态属性,它是Getter / Setter:

-(NSString*)propName:(NSString*)name {
    name = [name stringByReplacingOccurrencesOfString:@":" withString:@""];
    NSRange r;
    r.length = name.length -1 ;
    r.location = 1;
    NSString* firstChar = [name stringByReplacingCharactersInRange:r withString:@""];
    if([firstChar isEqualToString:[firstChar lowercaseString]])
    {return name;}
    r.length = 1;
    r.location = 0;
    NSString* theRest = [name stringByReplacingCharactersInRange:r withString:@""];
    return [NSString stringWithFormat:@"%@%@", [firstChar lowercaseString] , theRest];
}

-(NSString*)setterName:(NSString*)name {
    name = [self propName:name];
    NSRange r;
    r.length = name.length -1 ;
    r.location = 1;
    NSString* firstChar = [name stringByReplacingCharactersInRange:r withString:@""];
    r.length = 1;
    r.location = 0;
    NSString* theRest = [name stringByReplacingCharactersInRange:r withString:@""];
    return [NSString stringWithFormat:@"set%@%@", [firstChar uppercaseString] , theRest];
}


-(NSString*)propNameFromSetterName:(NSString*)name {
    NSRange r;
    r.length = 3 ;
    r.location = 0;
    NSString* propName = [name stringByReplacingCharactersInRange:r withString:@""];
    return [self propName:propName];  }


-(NSString*)ivarName:(NSString*)name  {
    NSRange r;
    r.length = name.length -1 ;
    r.location = 1;
    NSString* firstChar = [name stringByReplacingCharactersInRange:r withString:@""].lowercaseString;

 if([firstChar isEqualToString:@"_"])
        return name;
    r.length = 1;
    r.location = 0;
    NSString* theRest = [name stringByReplacingCharactersInRange:r withString:@""];
    return [NSString stringWithFormat:@"_%@%@",firstChar, theRest];  }


NSObject *getter(id self, SEL _cmd)
{
    NSString* name = NSStringFromSelector(_cmd);
    NSString* ivarName = [self ivarName:name];
    Ivar ivar = class_getInstanceVariable([self class], [ivarName UTF8String]);
    return object_getIvar(self, ivar);
}

void setter(id self, SEL _cmd, NSObject *newObj)
{
    NSString* name = [self propNameFromSetterName:NSStringFromSelector(_cmd)];
    NSString* ivarName = [self ivarName:name];
    Ivar ivar = class_getInstanceVariable([self class], [ivarName UTF8String]);
    id oldObj = object_getIvar(self, ivar);
    if (![oldObj isEqual: newObj])
    {
        object_setIvar(self, ivar, newObj);
        [newObj copy];
    }
}

-(NSDictionary *)createProperties:(NSArray *)propNames {
    NSMutableDictionary* keys = [[NSMutableDictionary alloc]init];
    for(NSString* key in propNames)
    {
        NSString* propName = [self propName: key];
        NSString* iVarName = [self ivarName:propName];

        class_addIvar([self class], [iVarName UTF8String] , sizeof(NSObject*), log2(sizeof(NSObject*)), @encode(NSObject));

        objc_property_attribute_t a1 = { "T", "@\"NSObject\"" };
        objc_property_attribute_t a2 = { "&", "" };
        objc_property_attribute_t a3 = { "N", "" };
        objc_property_attribute_t a4 = { "V", [iVarName UTF8String] };
        objc_property_attribute_t attrs[] = { a1, a2, a3, a4};

        class_addProperty([self class], [propName UTF8String], attrs, 4);
        class_addMethod([self class], NSSelectorFromString(propName), (IMP)getter, "@@:");
        class_addMethod([self class], NSSelectorFromString([self setterName:propName]), (IMP)setter, "v@:@");

        id val = [self.objectProperties objectForKey:key];
        [keys setValue:val forKey:propName];
    }
    return keys;
}

但是当用户想要观察任何属性时,在用户类中无法访问它。

我很困惑如何创建动态属性和应用KVO。

我还试过创建Entry类的子类即(用户在其末尾创建的myEntry)并在那里定义所有变量。但我如何在那里设定它的价值?因为我想要阅读该属性。

1 个答案:

答案 0 :(得分:1)

首先:在实例化类之后,您无法创建ivars。这是因为已经创建的实例的内存占用量会发生变化。因此-createProperties:是一种实例方法令人惊讶。

此外,KVO通过在运行时子类化观察实例的类并将新类分配给观察到的实例(isa-swizzling)来工作。如果在注册KVO后更改基类,这显然是个问题。

然而,所有说法都适用于自动KVO。您可以使用手动KVO。只需将KVO通知添加到您的访问者。

但是评论说:你应该重新思考你的整个概念。