gpt4 book ai didi

ios - EXC_BAD_ACCESS 尝试使用 object_getIvar 检索值时

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:39:54 24 4
gpt4 key购买 nike

我正在尝试学习如何利用 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";
}

首先,我创建了一个运行时类,其中包含与字典中每个对象的键关联的 ivar(例如 ivars = recipe_description、recipe_id、recipe_image 等)。其次,我将运行时类中每个 Ivar 的值设置为字典中每个对应的对象(例如 recipe_description = Delicious and healthy 等)。最后,我检索了 Ivar 的值。

我可以检索 recipe_name、recipe_id 和 recipe_description 的值,但无法检索 recipe_url 和 recipe_image 的值。当我尝试检索这些值时, 中的 value = object_getIvar(classInstance, ivar); 行出现 EXC_BAD_ACCESS code=2, address=0x5 错误valueForIvarContainingName:class: 方法。

代码:

- (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]);

为什么我可以检索 recipe_name、recipe_description、recipe_id 值,但不能检索 recipe_url 和 recipe_image 值?

我猜它可能与作为 URL 的对象有关?我试过将字典中的每个对象都转换为字符串,但没有任何效果。

非常感谢任何帮助!

最佳答案

我最终改用了 valueForKey:,这可能是更好的选择。

例如

id value = [MyCustomClassInstance valueForKey:@"key"];

关于ios - EXC_BAD_ACCESS 尝试使用 object_getIvar 检索值时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22309532/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com