Created
August 27, 2010 15:58
-
-
Save andrewsardone/553633 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// NSObject+PropertyListing.h | |
// PropertyFun | |
// | |
// Created by Andrew Sardone on 8/27/10. | |
// | |
#import <Foundation/Foundation.h> | |
@interface NSObject (PropertyListing) | |
// aps suffix to avoid namespace collsion | |
// ...for Andrew Paul Sardone | |
- (NSDictionary *)properties_aps; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// NSObject+PropertyListing.m | |
// PropertyFun | |
// | |
// Created by Andrew Sardone on 8/27/10. | |
// | |
#import "NSObject+PropertyListing.h" | |
#import <objc/runtime.h> | |
@implementation NSObject (PropertyListing) | |
- (NSDictionary *)properties_aps { | |
NSMutableDictionary *props = [NSMutableDictionary dictionary]; | |
unsigned int outCount, i; | |
objc_property_t *properties = class_copyPropertyList([self class], &outCount); | |
for (i = 0; i < outCount; i++) { | |
objc_property_t property = properties[i]; | |
NSString *propertyName = [[[NSString alloc] initWithCString:property_getName(property)] autorelease]; | |
id propertyValue = [self valueForKey:(NSString *)propertyName]; | |
if (propertyValue) [props setObject:propertyValue forKey:propertyName]; | |
} | |
free(properties); | |
return props; | |
} | |
@end |
i'd also add this:
NSString *propertyName = [[[NSString alloc] initWithCString:property_getName(property)] autorelease];
id propertyValue = [self valueForKey:(NSString *)propertyName];
if(propertyValue != nil)
[props setObject:propertyValue forKey:propertyName];
no deprication note, and handles exception on nil properties
Also a good catch, reflog. Updated in 0ae97c3
You really made my day, thanks so much for this!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good catch, dotmaster. Updated in 03fdac51