Cocoa: Getting the class of an attribute using a string

Discussion in 'Mac Programming' started by MacDonaldsd, Jul 15, 2008.

  1. MacDonaldsd macrumors 65816

    MacDonaldsd

    Joined:
    Sep 8, 2005
    Location:
    London , UK
    #1
    Ok so this may sound a bit wired, but I am creating an application/framework that needs to convert XML to its appropriate Data type in an Objective-C class.

    Code:
    
    if([ [dictionary objectForKey:key] isKindOfClass: [NSString class]]==YES )
    			
    {				
    //Set the variable
    }
    		
    if([ [dictionary objectForKey:key] isKindOfClass: [NSNumber class]]==YES )
    {
    
    //Set the variable
    
    }
    			
    
    [dictionary objectForKey:key]
    returns the name of the attribute that is going to be updated, but obviously this a string its self, so the above comparison is always a string. I need the actual class of the attribute and I cant find a way of doing it, anyone have any ideas ?
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    I may be oversimplifying this, but don't you just have a clearly defined name for each type in the XML? So for example you would have things in your dictionary like:
    Number
    String
    RealNumber

    It seems like you would just need to do isEqualToString: and compare to some NSString literals to find what type it is. Alternately, you could build your own dictionary whose keys are NSStrings that appear in the XML, and whose values are the Class that you'd like. Then you would just pass the NSString to objectForKey, and the result would be the Class you'd like to use.

    If this doesn't cover what you're trying to do, you might want to explain a bit more.

    -Lee
     
  3. MacDonaldsd thread starter macrumors 65816

    MacDonaldsd

    Joined:
    Sep 8, 2005
    Location:
    London , UK
    #3
    Hi Lee,

    my dictionary is like this:

    [dictionary setObject:mad:"cocoaName" forKey:mad:"xml_tag"];

    so for each XML tag I know the corresponding variable in my application, as the names differ due to reserved words etc.

    So for example if the variable is an NSDate, I would need to convert the text from the XML file into a NSDate using the date with natural language string method.
     
  4. ghayenga macrumors regular

    Joined:
    Jun 18, 2008
    #4
    It would seem that you are approaching this backwards. This method you seem to be describing would be for converting cocoa objects into XML not the other way around.

    To convert *from* XML to cocoa objects just use a simple case statement:

    In a loop:

    NSString *numberClass = @"number";
    NSString *stringClass = @"string";

    NSString *xmlTagString = [dictionary objectForKey:key];

    if([numberClass isEqualTo:xmlTagString )
    {
    NSNumber *numberObject = [[NSNumber alloc] initWithString:valueString];
    }

    if([stringClass isEqualTo:xmlTagString )
    {
    NSString *stringObject = [[NSString alloc] initWithString:valueString];
    }

    etc.
     
  5. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    I might still be missing something, but if that's the case it seems like you would do something like:

    Code:
    +(id)xmlDataToNativeType:(NSString *) tag withValue:(NSString *) value {
      NSString myType = [dictionary objectForKey:tag];
      if([myType isEqualToString:@"NSDate"]) {
        return [NSDate dateWithNaturalLanguageString:value];
      } else if([myType isEqualToString:@"NSNumber"]) {
        return [NSNumber numberWithInt:[value intValue]];
      } else if(...) {
        ...
      }
    }
    
    -Lee
     
  6. MacDonaldsd thread starter macrumors 65816

    MacDonaldsd

    Joined:
    Sep 8, 2005
    Location:
    London , UK
    #6
    Yeah I think I just need another method as you have suggested, to work it out.
     

Share This Page