ios - EXC_BAD_ACCESS when attempting to retrieve value using object_getIvar -
i trying learn how take advantage of objective-c runtime functions.
i have dictionary contains several name=value pairs.
e.g.
{ "recipe_description" = "delicious , 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"; } first, create runtime class contains ivars associated keys of each object in dictionary (e.g. ivars = recipe_description, recipe_id, recipe_image, etc). second, set values of each ivar in runtime class each corresponding object in dictionary (e.g. recipe_description = delicious , healthy, etc). finally, retrieve value of ivar.
i can retrieve values of recipe_name, recipe_id, , recipe_description, unable retrieve values of recipe_url , recipe_image. when attempt retrieve these values exc_bad_access code=2, address=0x5 error on line value = object_getivar(classinstance, ivar); in valueforivarcontainingname:class: method.
code:
- (class)wrapobjectwithname:(nsstring *)name ivarnames:(nsarray *)ivarnames { const char *classname = [name cstringusingencoding:nsasciistringencoding]; class objectclass = objc_allocateclasspair([nsobject class], classname, 0); (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]; (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; (int = 0; < 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; } usage:
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]); why can retrieve recipe_name, recipe_description, recipe_id values, not recipe_url , recipe_image values?
i'm guessing has objects being urls, maybe? i've tried converting each object in dictionary string, has no effect.
any appreciated!
i ended using valueforkey: instead, better choice.
e.g.
id value = [mycustomclassinstance valueforkey:@"key"];
Comments
Post a Comment