14

I need to determine an object's property (passed by name) type in order to perform deserialization from XML. I have some general pseudo-code (however I am unsure of how to perform these comparisons in Objective-C):

id object = [[[Record alloc] init] autorelease];

NSString *element = @"date";
NSString *data = @"2010-10-16";

objc_property_t property = class_getProperty([object class], [element UTF8String]);
const char *attributes = property_getAttributes(property);

char buffer[strlen(attributes) + 1];
strcpy(buffer, attributes);

char *attribute = strtok(buffer, ",");
if (*attribute == 'T') attribute++; else attribute = NULL;

if (attribute == NULL);
else if (strcmp(attribute, "@\"NSDate\"") == 0) [object setValue:[NSDate convertToDate:self.value] forKey:element];
else if (strcmp(attribute, "@\"NSString\"") == 0) [object setValue:[NSString convertToString:self.value] forKey:element];
else if (strcmp(attribute, "@\"NSNumber\"") == 0) [object setValue:[NSNumber convertToNumber:self.value] forKey:element];

I have looked through the class_getProperty and property_getAttributes, however I am still not sure how to do the above comparisons.

Kevin Sylvestre
  • 37,288
  • 33
  • 152
  • 232

3 Answers3

37

@Ahruman's answer is correct, if you're dealing with objects. Let me suggest some alternatives:

  1. valueForKey:: If you use [myObject valueForKey:@"myPropertyName"], it will return an object. If the property corresponds to some sort of primitive (int, float, CGRect, etc), then it will be boxed for you into an NSNumber or NSValue (as appropriate). If it comes back as an NSNumber, you can then easily extract a double representation (doubleValue) and use that as an NSTimeInterval to create an NSDate. I would probably recommend this approach.
  2. Special case each type. property_getAttributes() returns a char* representing all of the attributes of the property, and you can extract the type by doing this:

    const char * type = property_getAttributes(class_getProperty([self class], "myPropertyName"));
    NSString * typeString = [NSString stringWithUTF8String:type];
    NSArray * attributes = [typeString componentsSeparatedByString:@","];
    NSString * typeAttribute = [attributes objectAtIndex:0];
    NSString * propertyType = [typeAttribute substringFromIndex:1];
    const char * rawPropertyType = [propertyType UTF8String];
    
    if (strcmp(rawPropertyType, @encode(float)) == 0) {
      //it's a float
    } else if (strcmp(rawPropertyType, @encode(int)) == 0) {
      //it's an int
    } else if (strcmp(rawPropertyType, @encode(id)) == 0) {
      //it's some sort of object
    } else ....

    This is pedantically more correct than Louis's answer, because while most types have a single-character encoding, they don't have to. (his suggestion assumes a single-character encoding)

  3. Finally, if you're doing this on a subclass of NSManagedObject, then I would encourage checking out NSPropertyDescription.

From these alternatives, you can probably see that letting the runtime box the value for you is probably simplest.

edit extracting the type:

From the code above, you can extract the class name like so:

if ([typeAttribute hasPrefix:@"T@"] && [typeAttribute length] > 1) {
  NSString * typeClassName = [typeAttribute substringWithRange:NSMakeRange(3, [typeAttribute length]-4)];  //turns @"NSDate" into NSDate
  Class typeClass = NSClassFromString(typeClassName);
  if (typeClass != nil) {
    [object setValue:[self convertValue:self.value toType:typeClass] forKey:element];
  }
}

And then instead of using class method categories to do the conversion (ie, [NSDate convertToDate:]), make a method on self that does that for you and accepts the desired type as a parameter. You could (for now) do it like:

- (id) convertValue:(id)value toType:(Class)type {
  if (type == [NSDate class]) {
    return [NSDate convertToDate:value];
  } else if (type == [NSString class]) {
    return [NSString convertToString:value];
  } ...
}

Part of me is wondering, though: why on earth are you needing to do things this way? What are you making?

holex
  • 23,961
  • 7
  • 62
  • 76
Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
  • +1 valueForKey:/setValueForKey:. The autoboxing/unboxing uses NSNumber for basic types and NSValue for compound types (i.e. structs); for more details, see http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/KeyValueCoding/Concepts/DataTypes.html – tc. Aug 16 '10 at 23:08
  • Thanks Dave for the detailed response! I am not sure how to utilize the first method (won't valueForKey return nil until I've finished decoding the object, as all the properties will be nil? The second method (the method I have been attempting), works for scalars, but doesn't appear to work for non-scalars. For example, '@encode(NSDate *)' gives "@", but getting the attributes for '@property (nonatomic, retain) NSDate *date' gives "T@"NSDate",&,N,Vdate" (not sure why the attribute section isn't the same, it says it uses @encode). Any ideas? I updated my question with my latest code. Thanks! – Kevin Sylvestre Aug 16 '10 at 23:36
  • 1
    @Kevin yes, the 1st option requires an instance of the object. As for the second, I forgot that property encodes a bit more about the type than `@encode` does. However, you could use this your advantage. If the first character is `@`, then see if you can extract a class name from between the `"` marks. – Dave DeLong Aug 16 '10 at 23:59
  • Thanks Dave, I did one more update to my code with my tentative final version. Just one last question, do you know if a similar compiler directive to '@encode' exists that will result in @encode(NSString *) = '@"NSDate"'? Hard coding these values (as I did above) doesn't seem like the ideal solution. Thanks again for the help! – Kevin Sylvestre Aug 17 '10 at 00:17
  • @DaveDeLong I've edited your answer slightly. When putting the "extracting the type" part of this code into use, I noticed you are looking for a string like `@"NSArray"` when the actual string in `typeAttribute` is like `T@"NSArray"`. I've added the `T` and adjusted the `NSRange` accordingly. Thanks for this example! – Kenny Winker Nov 09 '12 at 21:40
  • Hi Dave. Thanks for this. I'm just concerned about the use of the hard-coded range `(3, [typeAttribute length]-4)`. Is this guaranteed safe? Will the type name *always* be 3 characters in and 4 from the end of the first comma-delimited component of the encoding? Cheers. – devios1 Nov 27 '14 at 18:43
3

If you only care about classes you can use isKindOfClass:, but if you want to deal with scalars you are correct that you need to use property_getAttributes(), it returns a string that encodes the type information. Below is a basic function that demonstrates what you need to do. For examples of encoding strings, look here.

unsigned int outCount, i;
objc_property_t *properties = class_copyPropertyList([object class], &outCount);
for (i = 0; i < outCount; i++) {
    objc_property_t property = properties[i];
    char *property_name = property_getName(property);
    char *property_type = property_getAttributes(property);

    switch(property_type[1]) {
      case 'f' : //float
        break;
      case 's' : //short
        break;
      case '@' : //ObjC object
        //Handle different clases in here
        break;
    }
}

Obvviously you will need to add all the types and classes you need to handle to this, it uses the normal ObjC @encode types.

Louis Gerbarg
  • 43,356
  • 8
  • 80
  • 90
  • Note that property attributes aren't just Objective-C type encodings, they also encode information about the property itself (e.g. the names of non-default getters & setters it represents, its atomicity and memory management behavior, etc.). – Chris Hanson Aug 21 '10 at 10:00
  • True, though in the above example I am keying off of the second char in the string, which is the type info. The rest of info requires more complex parsing code. – Louis Gerbarg Aug 23 '10 at 02:03
0

[object isKindOfClass:[NSDate class]], etc.

Jens Ayton
  • 14,532
  • 3
  • 33
  • 47