Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 998381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:11:05+00:00 2026-05-16T07:11:05+00:00

I need to determine an object’s property (passed by name) type in order to

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T07:11:06+00:00Added an answer on May 16, 2026 at 7:11 am

    @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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.