title: EXC_BAD_ACCESS when attempting to retrieve value using object_getIvar
body: <p>I am trying to learn how to take advantage of the objective-c runtime functions.</p>
  
  <p>I have a dictionary that contains several <code>name=value</code> pairs.</p>
  
  <p><strong>E.g.</strong></p>
  
  <pre><code>{
      "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 &amp; Cherry Tomato Cooked Salad";
      "recipe_url" = "http://www.fatsecret.com/recipes/brown-rice-and-cherry-tomato-cooked-salad/Default.aspx";
  }
  </code></pre>
  
  <p>First, I create a runtime class which contains ivars associated with the keys of each object in the dictionary (e.g. ivars = recipe_description, recipe_id, recipe_image, etc). Second, I set the values of each Ivar in the runtime class to each corresponding object in the dictionary (e.g. recipe_description = Delicious and healthy, etc). Finally, I retrieve the value of Ivar.</p>
  
  <p>I can retrieve the values of recipe_name, recipe_id, and recipe_description, but I am unable to retrieve the values of recipe_url and recipe_image. When I attempt to retrieve these values I get a <code>EXC_BAD_ACCESS code=2, address=0x5</code> error on the line <code>value = object_getIvar(classInstance, ivar);</code> in the <code>valueForIvarContainingName:class:</code> method.</p>
  
  <p><strong>Code:</strong></p>
  
  <pre><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], &amp;outCount);
  
      id value;
  
      for (int i = 0; i &lt; 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;
  }
  </code></pre>
  
  <p><strong>Usage:</strong></p>
  
  <pre><code>         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]);
  </code></pre>
  
  <p>Why can I retrieve the recipe_name, recipe_description, recipe_id values, but not the recipe_url and recipe_image values?</p>
  
  <p>I'm guessing it has something to do with the objects being URLs, maybe? I've tried converting each object in the dictionary to a string, but that has no effect.</p>
  
  <p>Any help is greatly appreciated!</p>