Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MacDonaldsd

macrumors 65816
Original poster
Sep 8, 2005
1,005
0
London , UK
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 ?
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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 ?

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
 

MacDonaldsd

macrumors 65816
Original poster
Sep 8, 2005
1,005
0
London , UK
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.
 

ghayenga

macrumors regular
Jun 18, 2008
190
0
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.

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.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.