iOS - 运行时动态 Class 不符合键值编码

iOS - Runtime Dynamic Class not key-value coding compliant

我一直在加快在 Objective-C 运行时创建动态 Class 的速度,并且遇到了一个我似乎无法完全解决的问题。找了好久,还是一头雾水。

这里的问题是:

[runtimeClassInstance setValue:age forKey:@"age"];

调用实例化对象:

id runtimeClassInstance = [[NSClassFromString(@"Employee") alloc] init];

创建者

[NewClassMaker buildClassFromDictionary:@[@"FirstName", @"LastName",\
                                          @"Age", @"Salary"]
                               withName:@"Employee"];

这是一个 NSUnknownKeyException:这个 class 不符合密钥年龄 错误的密钥值编码。

如果我使用以下调用方法,一切正常:

SEL setAgeSelector = NSSelectorFromString(@"setAge");
NSInvocation *call = [NSInvocation invocationWithMethodSignature:[[NSClassFromString(@"Employee") class] instanceMethodSignatureForSelector:setAgeSelector]];
call.target = runtimeClassInstance;
call.selector = setAgeSelector;
NSNumber *age = @(25);
[call setArgument:&age atIndex:2];
[call invoke];

现在创建 class 的静态方法是这样的:

+(NSDictionary*)buildClassFromDictionary:(NSArray*)propNames withName:(NSString*)className
{
    NSMutableDictionary* keys = [[NSMutableDictionary alloc]init];
    Class newClass = NSClassFromString(className);

    if(newClass == nil)
    {
        newClass = objc_allocateClassPair([NSObject class], [className UTF8String], 0);

        // For each property name, add a new iVar, getter, and setter method for it.
        for(NSString* key in propNames)
        {
            NSString* propName = [self propName: key];
            NSString* iVarName = [self ivarName:propName];

            class_addIvar(newClass, [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(newClass, [propName UTF8String], attrs, 4);
            class_addMethod(newClass, NSSelectorFromString(propName), (IMP)getter, "@@:");
            class_addMethod(newClass, NSSelectorFromString([self setterName:propName]), (IMP)setter, "v@:@");

            [keys setValue:key forKey:propName];
        }

        Class metaClass = object_getClass(newClass);
        // This method is returning NO on purpose to find out why there is no key.
        class_addMethod(metaClass, @selector(accessInstanceVariablesDirectly), (IMP)accessInstanceVariablesDirectly, "B@:");

        // Auxilliary methods added to the new class instance so the accessor dynamic methods above can work.
        // Not sure if the initial impl of this class maker class worked.
        class_addMethod(newClass, @selector(ivarName:), (IMP)ivarNameForString, "@@:@");
        class_addMethod(newClass, @selector(propName:), (IMP)propNameForString, "@@:@");
        class_addMethod(newClass, @selector(propNameFromSetterName:), (IMP)propNameFromSetterNameString, "@@:@");

        // Add a customized description dynamic method to this class. It will dump out any list of properties added
        // to the object during init here.
        Method description = class_getInstanceMethod([NSObject class],
                                                     @selector(description));
        const char *types = method_getTypeEncoding(description);

        // now add
        class_addMethod(newClass, @selector(description), (IMP)Description, types);
        objc_registerClassPair(newClass);
    }
    return keys;
}

我正在考虑三种可能性:

  1. 我在这里错过了获取这些属性的一些关键步骤 他们的动态访问器以兼容键值的方式工作
  2. 如果我允许直接 iVar 访问,是否保留值 发生了什么?
  3. NSInvocation 方法是最好的方法吗?

setter 方法被命名为 setAgesetValue:forKey: 搜索带冒号的 setAge:。在 setter 方法的名称末尾添加一个冒号。在 Objective-C 中,冒号是方法名称的一部分。