4

I have the following class definition.

Contact.h

#import <CoreData/CoreData.h>


@interface Contact :  NSManagedObject  
{
}

@property (nonatomic, retain) NSString * City;
@property (nonatomic, retain) NSDate * LastUpdated;
@property (nonatomic, retain) NSString * Country;
@property (nonatomic, retain) NSString * Email;
@property (nonatomic, retain) NSNumber * Id;
@property (nonatomic, retain) NSString * ContactNotes;
@property (nonatomic, retain) NSString * State;
@property (nonatomic, retain) NSString * StreetAddress2;
@property (nonatomic, retain) NSDate * DateCreated;
@property (nonatomic, retain) NSString * FirstName;
@property (nonatomic, retain) NSString * Phone1;
@property (nonatomic, retain) NSString * PostalCode;
@property (nonatomic, retain) NSString * Website;
@property (nonatomic, retain) NSString * StreetAddress1;
@property (nonatomic, retain) NSString * LastName;

@end

Is it possible to obtain an array of NSString objects with all of the properties by name?

Array would look like this...

[@"City", @"LastUpdated", @"Country", .... ]

Solution (updated based on comment)

Thanks to Dave's answer I was able to write the following method to be used

- (NSMutableArray *) propertyNames: (Class) class { 
    NSMutableArray *propertyNames = [[NSMutableArray alloc] init];
    unsigned int propertyCount = 0;
    objc_property_t *properties = class_copyPropertyList(class, &propertyCount);

    for (unsigned int i = 0; i < propertyCount; ++i) {
        objc_property_t property = properties[i];
        const char * name = property_getName(property);
        [propertyNames addObject:[NSString stringWithUTF8String:name]];
    }
    free(properties);
    return [propertyNames autorelease];
}
5
  • 1
    You should return [propertyNames autorelease]; to follow memory management guidelines. Also, why not just pass in a Class object instead of a string to convert to a Class object? Commented Jun 8, 2010 at 22:49
  • This should also be a free function or a class method, rather than an instance method. Commented Jun 9, 2010 at 4:04
  • @dreamlax does having it as a class method make much of a difference if I am using it privately within a singleton? (still new in objc) Commented Jun 9, 2010 at 4:16
  • 2
    It is more logical for it to be a class method because it doesn't depend on the state of an instance (it doesn't reference any instance variables). If a method doesn't depend on the state of an instance, it should be a class method. The output is entirely dependent only on the class parameter, so it should be a class method or free function. Alternatively you could make it a class category method that operates on the current class. Commented Jun 9, 2010 at 4:36
  • Well, actually that's not entirely true, but generally speaking it is the case. Commented Jun 9, 2010 at 4:41

2 Answers 2

5

Yep! Here you go:

#import <objc/runtime.h>

//somewhere:
unsigned int propertyCount = 0;
objc_property_t * properties = class_copyPropertyList([self class], &propertyCount);

NSMutableArray * propertyNames = [NSMutableArray array];
for (unsigned int i = 0; i < propertyCount; ++i) {
  objc_property_t property = properties[i];
  const char * name = property_getName(property);
  [propertyNames addObject:[NSString stringWithUTF8String:name]];
}
free(properties);
NSLog(@"Names: %@", propertyNames);

Warning: code typed in browser.

Sign up to request clarification or add additional context in comments.

Comments

2

You could call dictionaryWithValuesForKeys: and then call the allValues method on that dictionary to get an array of values. Note that you'll need to convert the non-NSString properties to strings yourself.

2 Comments

That gives you the values of the specified keys. It seems like @Flash84x wants a list of keys.
What would I pass to dictionaryWithValuesForKeys:?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.