Skip to content

Instantly share code, notes, and snippets.

@IgorMuzyka
Created December 17, 2014 19:47
Show Gist options
  • Save IgorMuzyka/c0ee39cad606f2ca0738 to your computer and use it in GitHub Desktop.
Save IgorMuzyka/c0ee39cad606f2ca0738 to your computer and use it in GitHub Desktop.
c methods to get class properties classes from properties attributes
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
#import "AppDelegate.h"
#import <objc/runtime.h>
static const char *getPropertyType(objc_property_t property) {
const char *attributes = property_getAttributes(property);
//printf("attributes=%s\n", attributes);
char buffer[1 + strlen(attributes)];
strcpy(buffer, attributes);
char *state = buffer, *attribute;
while ((attribute = strsep(&state, ",")) != NULL) {
if (attribute[0] == 'T' && attribute[1] != '@') {
// it's a C primitive type:
/*
if you want a list of what will be returned for these primitives, search online for
"objective-c" "Property Attribute Description Examples"
apple docs list plenty of examples of what you get for int "i", long "l", unsigned "I", struct, etc.
*/
NSString *name = [[NSString alloc] initWithBytes:attribute + 1 length:strlen(attribute) - 1 encoding:NSASCIIStringEncoding];
return (const char *)[name cStringUsingEncoding:NSASCIIStringEncoding];
}
else if (attribute[0] == 'T' && attribute[1] == '@' && strlen(attribute) == 2) {
// it's an ObjC id type:
return "id";
}
else if (attribute[0] == 'T' && attribute[1] == '@') {
// it's another ObjC object type:
NSString *name = [[NSString alloc] initWithBytes:attribute + 3 length:strlen(attribute) - 4 encoding:NSASCIIStringEncoding];
return (const char *)[name cStringUsingEncoding:NSASCIIStringEncoding];
}
}
return "";
}
NSDictionary *classPropertiesClassesByPropertiesNames(__unsafe_unretained Class class) {
NSMutableDictionary *dictionary = [NSMutableDictionary new];
unsigned count;
objc_property_t *properties = class_copyPropertyList(class, &count);
for (unsigned i = 0; i < count; ++i) {
objc_property_t property = properties[i];
NSString *propertyName = [NSString stringWithUTF8String:property_getName(property)];
// NSString *propertyAttributes = [NSString stringWithUTF8String:property_getAttributes(property)];
NSString *propertyType = [NSString stringWithUTF8String:getPropertyType(property)];
[dictionary setObject:propertyType forKey:propertyName];
}
free(properties);
return [NSDictionary dictionaryWithDictionary:dictionary];
}
@interface PPRefixSomeRandomClassToCheckTypes : NSObject
@property (nonatomic, strong) id unknownIdentifier;
@property (nonatomic, strong) NSArray *array;
@property (nonatomic, strong) NSString *string;
@property (nonatomic, strong) NSDictionary *dictionary;
@property (nonatomic, assign) NSInteger integerValue;
@property (nonatomic, assign) CGFloat floatValue;
@property (nonatomic, assign) CGRect rectStructValue;
@property (nonatomic, assign) BOOL boolValue;
@end
@implementation PPRefixSomeRandomClassToCheckTypes
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSDictionary *dictionary = classPropertiesClassesByPropertiesNames([PPRefixSomeRandomClassToCheckTypes class]);
NSLog(@"%@", dictionary);
return YES;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment