Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ssl/3.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 EXC_尝试使用object_getIvar检索值时访问错误_Ios_Objective C Runtime - Fatal编程技术网

Ios EXC_尝试使用object_getIvar检索值时访问错误

Ios EXC_尝试使用object_getIvar检索值时访问错误,ios,objective-c-runtime,Ios,Objective C Runtime,我试图学习如何利用objective-c运行时函数 我有一本字典,里面有几个name=value对 例如 { "recipe_description" = "Delicious and healthy."; "recipe_id" = 7042366; "recipe_image" = "http://www.fatsecret.com/static/recipe/b5b8ccb7-badd-4a7f-8dd4-0ffe4aba8c6d.jpg"; "recipe

我试图学习如何利用objective-c运行时函数

我有一本字典,里面有几个
name=value

例如

{
    "recipe_description" = "Delicious and healthy.";
    "recipe_id" = 7042366;
    "recipe_image" = "http://www.fatsecret.com/static/recipe/b5b8ccb7-badd-4a7f-8dd4-0ffe4aba8c6d.jpg";
    "recipe_name" = "Brown Rice & Cherry Tomato Cooked Salad";
    "recipe_url" = "http://www.fatsecret.com/recipes/brown-rice-and-cherry-tomato-cooked-salad/Default.aspx";
}
id value = [MyCustomClassInstance valueForKey:@"key"];
首先,我创建一个运行时类,其中包含与字典中每个对象的键相关联的IVAR(例如ivars=recipe\u description、recipe\u id、recipe\u image等)。其次,我将运行时类中每个Ivar的值设置为字典中每个对应的对象(例如,recipe_description=Delicious and Health等)。最后,我检索Ivar的值

我可以检索配方名称、配方id和配方描述的值,但无法检索配方url和配方图像的值。当我试图检索这些值时,我在
value=object\u getIvar(classInstance,ivar)行上得到一个
EXC\u BAD\u ACCESS code=2,address=0x5
错误valueForIvarContainingName:class:
方法中的code>

代码:

- (Class)wrapObjectWithName:(NSString *)name ivarNames:(NSArray *)ivarNames
{
    const char *className = [name cStringUsingEncoding:NSASCIIStringEncoding];

    Class objectClass = objc_allocateClassPair([NSObject class], className, 0);

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

        class_addIvar(objectClass, iVarName, sizeof(NSString*), log2(sizeof(NSString*)), @encode(NSString*));
    }

    objc_registerClassPair(objectClass);

    return objectClass;
}

- (void)mapValues:(NSDictionary *)dictionary toVariablesInClass:(id)classInstance
{
    NSArray *dictionaryObjectKeys = [dictionary allKeys];

    for (NSString *key in dictionaryObjectKeys)
    {
        const char *iVarName = [key cStringUsingEncoding:NSASCIIStringEncoding];
        Ivar ivar = class_getInstanceVariable([classInstance class], iVarName);

        id value = dictionary[key];

        object_setIvar(classInstance, ivar, value);
    }
}

- (id)valueForIvarContainingName:(NSString *)anIvarName class:(id)classInstance
{
    unsigned int outCount;
    Ivar *iVarList = class_copyIvarList([classInstance class], &outCount);

    id value;

    for (int i = 0; i < outCount; i++)
    {
        Ivar ivar = iVarList[i];

        NSString *ivarName = [NSString stringWithCString:ivar_getName(ivar) encoding:NSASCIIStringEncoding];

        if ([ivarName rangeOfString:anIvarName].location != NSNotFound)
        {
            value = object_getIvar(classInstance, ivar);
            break;
        }
    }

    free(iVarList);

    return value;
}
         NSArray *ivarNames = [dictionary allKeys];
         Class FSRecipe = [self wrapObjectWithName:@"FSRecipe" ivarNames:ivarNames];

         id recipe = [[FSRecipe alloc] init];
         [self mapValues:dictionary toVariablesInClass:recipe];

         NSLog(@"%@", [self valueForIvarContainingName:@"image" class:recipe]);
为什么我可以检索配方名称、配方描述、配方id值,而不能检索配方url和配方图像值

我猜这可能和作为URL的对象有关吧?我尝试将字典中的每个对象转换为字符串,但没有效果


非常感谢您的帮助

我最终使用了
valueForKey:
,这可能是一个更好的选择

例如

{
    "recipe_description" = "Delicious and healthy.";
    "recipe_id" = 7042366;
    "recipe_image" = "http://www.fatsecret.com/static/recipe/b5b8ccb7-badd-4a7f-8dd4-0ffe4aba8c6d.jpg";
    "recipe_name" = "Brown Rice & Cherry Tomato Cooked Salad";
    "recipe_url" = "http://www.fatsecret.com/recipes/brown-rice-and-cherry-tomato-cooked-salad/Default.aspx";
}
id value = [MyCustomClassInstance valueForKey:@"key"];

哎呀。我本想把它拿出来的,接得好。我正在将对象转换为字符串表示,看看这是否能修复EXC_BAD_访问;不管怎么说,这都不是问题的原因。我刚刚发现,出于某种原因,object_getIvar中的ivar为零,但ivar肯定存在,并且有一个值;您的代码在
[self-valueForIvarContainingName:@“image”类:recipe]
[self-valueForIvarContainingName:@“url”类:recipe]
中都能正常工作,这些IVAR的值是
NSURL
实例。@JoshCaswell,应用程序唯一的另一部分是将这些对象存储在数组中,然后在collectionViewCell上显示值。我猜你为什么没有重现同样的错误。