Skip to content

Instantly share code, notes, and snippets.

@uzysjung
Last active January 28, 2016 03:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uzysjung/59b7ab9092fc1870fb5a to your computer and use it in GitHub Desktop.
Save uzysjung/59b7ab9092fc1870fb5a to your computer and use it in GitHub Desktop.
//
// ClassInfo.m
// sentinelShuttleValidationCheck
//
// Created by uzysjung on 2016. 1. 28..
// Copyright © 2016년 skpdi. All rights reserved.
//
#import "ClassInfo.h"
#import "RakeClientMetricSentinelShuttle.h"
#include <objc/runtime.h>
//https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html
@implementation ClassInfo
+ (void) run:(Class)class {
unsigned int count;
objc_property_t* props = class_copyPropertyList(class, &count);
for (int i = 0; i < count; i++) {
objc_property_t property = props[i];
const char * name = property_getName(property);
NSString *propertyName = [NSString stringWithCString:name encoding:NSUTF8StringEncoding];
const char * type = property_getAttributes(property);
NSString *attr = [NSString stringWithCString:type encoding:NSUTF8StringEncoding];
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) {
} else if (strcmp(rawPropertyType, @encode(int)) == 0) {
} else if (strcmp(rawPropertyType, @encode(id)) == 0) {
} else {
}
if ([typeAttribute hasPrefix:@"T@"]) {
NSString * typeClassName = [typeAttribute substringWithRange:NSMakeRange(3, [typeAttribute length]-4)]; //turns @"NSDate" into NSDate
Class typeClass = NSClassFromString(typeClassName);
if (typeClass != nil) {
// Here is the corresponding class even for nil values
}
}
NSLog(@"Name:%@ Type:%@",propertyName,propertyType);
}
free(props);
}
+ (NSString *)getClassPropertyType:(Class)class propertyName:(NSString *)pName {
NSString *ret = nil;
unsigned int count;
objc_property_t* props = class_copyPropertyList([RakeClientMetricSentinelShuttle class], &count);
for (int i = 0; i < count; i++) {
objc_property_t property = props[i];
const char * name = property_getName(property);
NSString *propertyName = [NSString stringWithCString:name encoding:NSUTF8StringEncoding];
const char * type = property_getAttributes(property);
NSString * typeString = [NSString stringWithUTF8String:type];
NSArray * attributes = [typeString componentsSeparatedByString:@","];
NSString * typeAttribute = [attributes objectAtIndex:0];
NSString * propertyType = [typeAttribute substringFromIndex:1];
const char * rawPropertyType = [propertyType UTF8String];
NSString *realType = [propertyType stringByReplacingOccurrencesOfString:@"@" withString:@""];
realType = [realType stringByReplacingOccurrencesOfString:@"\"" withString:@""];
// NSLog(@"Name:%@ Type:%@",propertyName,propertyType);
if([propertyName isEqualToString:pName]) {
ret = realType;
break;
}
}
free(props);
return ret;
}
+ (BOOL)classHasMethod:(Class)class methodName:(NSString *)mName {
BOOL ret = false;
int unsigned numMethods;
Method *methods = class_copyMethodList(class, &numMethods);
for (int i = 0; i < numMethods; i++) {
NSString *methodName =NSStringFromSelector(method_getName(methods[i]));
// NSLog(@"%@", methodName);
// char buffer[256];
// SEL name = method_getName(methods[i]);
// NSLog(@"Method: %@", NSStringFromSelector(name));
// char *returnType = method_copyReturnType(methods[i]);
// NSLog(@"The return type is %s", returnType);
// free(returnType);
// // self, _cmd + any others
// unsigned int numberOfArguments = method_getNumberOfArguments(methods[i]);
// for(int j=0; j<numberOfArguments; j++)
// {
// method_getArgumentType(methods[i], j, buffer, 256);
// NSLog(@"The type of argument %d is %s", j, buffer);
// }
if([methodName isEqualToString:mName]){
ret = true;
break;
}
}
free(methods);
return ret;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment