Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用object_setIvar时的EXC_错误访问_Ios_Objective C_Objective C Runtime - Fatal编程技术网

Ios 使用object_setIvar时的EXC_错误访问

Ios 使用object_setIvar时的EXC_错误访问,ios,objective-c,objective-c-runtime,Ios,Objective C,Objective C Runtime,我正在尝试在使用下面的代码分配的运行时类上添加和设置IVAR。我没有任何使用objective-c运行时函数的经验,这就是我试图学习的原因 if ([object isKindOfClass:[NSDictionary class]]) { const char *className = [aName cStringUsingEncoding:NSASCIIStringEncoding]; // Allocate the class using

我正在尝试在使用下面的代码分配的运行时类上添加和设置IVAR。我没有任何使用objective-c运行时函数的经验,这就是我试图学习的原因

    if ([object isKindOfClass:[NSDictionary class]])
    {
        const char *className = [aName cStringUsingEncoding:NSASCIIStringEncoding];

        // Allocate the class using the class name, NSObject metaclass, and a size of 0
        Class objectClass = objc_allocateClassPair([NSObject class], className, 0);

        // Get all of the keys in the dictionary to use as Ivars
        NSDictionary *dictionaryObject = (NSDictionary *)object;
        NSArray *dictionaryObjectKeys = [dictionaryObject allKeys];

        for (NSString *key in dictionaryObjectKeys)
        {
            // Convert the NSString to a C string
            const char *iVarName = [key cStringUsingEncoding:NSASCIIStringEncoding];

            // Add the Ivar to the class created above using the key as the name
            if (class_addIvar(objectClass, iVarName, sizeof(NSString*), log2(sizeof(NSString*)), @encode(NSString*)))
            {
                // Get the newly create Ivar from the class created above using the key as the name
                Ivar ivar = class_getInstanceVariable(objectClass, iVarName);

                // Set the newly created Ivar to the value of the key
                id value = dictionaryObject[key];
                object_setIvar(objectClass, ivar, [value copy]);
            }
        }

        objc_registerClassPair(objectClass);
    }
每次运行上述代码时,我都会在
object\u setIvar(objectClass,ivar,[value copy])行上得到一个EXC\u BAD\u访问(code=2,address=0x10)错误。我不明白为什么我会犯这个错误。在我尝试设置Ivar的值之前,我检查Ivar是否已成功添加到类中,但显然Ivar为零。我试图记录ivar,但我得到了同样的错误

我尝试在Google上搜索解决方案,但找不到有关objective-c运行时函数的更多信息


我正在使用ARC并在iOS模拟器上运行应用程序。

您无法为类上的实例变量设置值。 创建并注册该类后,可以创建该类的实例:

id myInstance = [[objectClass alloc] init];
然后在此对象上设置实例变量的值:

for (NSString *key in dictionaryObjectKeys)
{
    const char *iVarName = [key cStringUsingEncoding:NSASCIIStringEncoding];

    id value = dictionaryObject[key];
    Ivar ivar = class_getInstanceVariable(objectClass, iVarName);

    object_setIvar(myInstance, ivar, [value copy]);
}

哦,我明白了。这是有道理的。当我尝试从iVar(object_getIvar(instance,iVar))检索值时,它返回null,但我知道我设置的值不是null。你知道为什么会这样吗?@Jonathan:
idy=object\u getIvar(myInstance,ivar)在我尝试时返回以前设置的值。是否需要示例末尾附近的
[value copy]
?还是仅用于复制属性?你能把
value
传递到那里吗?@Olie:如果你不想要一个独立的实例,你可以把
value
传递到那里。我刚刚从问题中复制了(!)那部分。在将值赋给实例变量之前,有没有一种快速的方法可以在值上添加类型检查?如果类型不匹配,就不要设置它。