├── .gitignore ├── DHCObjectIntrospection.podspec ├── LICENSE ├── NSObject+Introspect ├── NSObject+introspect.h └── NSObject+introspect.m ├── NSObjectIntrospectionDemo.xcodeproj ├── project.pbxproj └── xcshareddata │ └── xcschemes │ └── NSObjectIntrospectionDemo.xcscheme ├── NSObjectIntrospectionDemo ├── AppDelegate.h ├── AppDelegate.m ├── Default-568h@2x.png ├── Default.png ├── Default@2x.png ├── DemoLeafObject.h ├── DemoLeafObject.m ├── DemoObject.h ├── DemoObject.m ├── NSObjectIntrospectionDemo-Info.plist ├── NSObjectIntrospectionDemo-Prefix.pch ├── ViewController.h ├── ViewController.m ├── en.lproj │ ├── InfoPlist.strings │ └── ViewController.xib └── main.m ├── NSObjectIntrospectionDemoTests ├── NSObject+IntrospectTests.h ├── NSObject+IntrospectTests.m ├── NSObjectIntrospectionDemoTests-Info.plist └── en.lproj │ └── InfoPlist.strings └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | .DS_Store 3 | build/ 4 | *.pbxuser 5 | !default.pbxuser 6 | *.mode1v3 7 | !default.mode1v3 8 | *.mode2v3 9 | !default.mode2v3 10 | *.perspectivev3 11 | !default.perspectivev3 12 | *.xcworkspace 13 | !default.xcworkspace 14 | xcuserdata 15 | profile 16 | *.moved-aside 17 | DerivedData 18 | .idea/ 19 | -------------------------------------------------------------------------------- /DHCObjectIntrospection.podspec: -------------------------------------------------------------------------------- 1 | Pod::Spec.new do |s| 2 | s.name = "DHCObjectIntrospection" 3 | s.version = "0.1.0" 4 | s.summary = "a category on NSObject to allow for runtime introspection." 5 | s.homepage = "https://github.com/confidenceJuice/NSObjectIntrospection" 6 | 7 | s.license = 'MIT' 8 | s.author = { "Daniel Haight" => "confidence.designe@gmail.com" } 9 | s.source = { :git => "https://github.com/confidenceJuice/NSObjectIntrospection.git", :tag => "#{s.version}" } 10 | s.source_files = 'NSObject+Introspect/*.{h,m}' 11 | s.framework = 'Foundation' 12 | 13 | s.requires_arc = true 14 | end 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | Copyright (c) 2013 Daniel Haight 3 | 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 18 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 19 | DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 20 | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 21 | OR OTHER DEALINGS IN THE SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /NSObject+Introspect/NSObject+introspect.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSObject+introspect.h 3 | // CJFoundation 4 | // 5 | // Created by confidence on 13/12/2012. 6 | // Copyright (c) 2012 confidence. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface NSObject (DHCIntrospect) 12 | 13 | -(NSDictionary *)propertiesDict __attribute((deprecated("use dhc_propertiesDict"))); 14 | -(NSDictionary *)dhc_propertiesDict; 15 | 16 | -(NSDictionary *)iVarsDict __attribute((deprecated("use dhc_iVarsDict"))); 17 | -(NSDictionary *)dhc_iVarsDict; 18 | 19 | -(NSDictionary *)methodsDict __attribute((deprecated("use dhc_methodsDict"))); 20 | -(NSDictionary *)dhc_methodsDict; 21 | 22 | -(NSDictionary *)objectIntrospectDictionary __attribute((deprecated("use dhc_objectIntrospectDictionary"))); 23 | -(NSDictionary *)dhc_objectIntrospectDictionary; 24 | 25 | @end -------------------------------------------------------------------------------- /NSObject+Introspect/NSObject+introspect.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSObject+introspect.m 3 | // CJFoundation 4 | // 5 | // Created by confidence on 13/12/2012. 6 | // Copyright (c) 2012 confidence. All rights reserved. 7 | // 8 | 9 | #import "NSObject+introspect.h" 10 | #import 11 | 12 | @implementation NSObject (DHCIntrospect) 13 | 14 | -(NSDictionary*)propertiesDict { 15 | return [self dhc_propertiesDict]; 16 | } 17 | 18 | -(NSDictionary*) dhc_propertiesDict { 19 | NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; 20 | 21 | NSUInteger outCount; 22 | 23 | objc_property_t *properties = class_copyPropertyList([self class], &outCount); 24 | 25 | for(NSInteger i = 0; i < outCount; i++) { 26 | objc_property_t property = properties[i]; 27 | const char *propName = property_getName(property); 28 | NSString *propertyNameString = [NSString stringWithUTF8String:propName]; 29 | NSValue *propertyValue = [self valueForKey:propertyNameString]; 30 | if (propertyValue){ 31 | [dict setValue:propertyValue forKey:propertyNameString]; 32 | } 33 | else { 34 | [dict setObject:[NSNull null] forKey:propertyNameString]; 35 | } 36 | } 37 | 38 | free(properties); 39 | 40 | return dict; 41 | } 42 | 43 | -(NSDictionary *)iVarsDict{ 44 | return [self dhc_iVarsDict]; 45 | } 46 | 47 | -(NSDictionary *)dhc_iVarsDict{ 48 | NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; 49 | 50 | NSUInteger outCount; 51 | 52 | Ivar* ivars = class_copyIvarList([self class], &outCount); 53 | for (NSInteger i = 0; i < outCount ; i++){ 54 | Ivar ivar=ivars[i]; 55 | const char* ivarName = ivar_getName(ivar); 56 | NSString *ivarNameString = [NSString stringWithUTF8String:ivarName]; 57 | NSValue *value = [self valueForKey:ivarNameString]; 58 | 59 | if (value) { 60 | [dict setValue:value forKey:ivarNameString]; 61 | } 62 | 63 | else { 64 | [dict setValue:[NSNull null] forKey:ivarNameString]; 65 | } 66 | } 67 | 68 | free(ivars); 69 | 70 | return dict; 71 | } 72 | 73 | -(NSDictionary *)methodsDict{ 74 | return [self dhc_methodsDict]; 75 | } 76 | 77 | -(NSDictionary *)dhc_methodsDict{ 78 | NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; 79 | 80 | NSUInteger outCount; 81 | 82 | Method* methods = class_copyMethodList([self class], &outCount); 83 | 84 | for (NSInteger i=0; i 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 33 | 39 | 40 | 41 | 42 | 43 | 49 | 50 | 51 | 52 | 61 | 62 | 68 | 69 | 70 | 71 | 72 | 73 | 79 | 80 | 86 | 87 | 88 | 89 | 91 | 92 | 95 | 96 | 97 | -------------------------------------------------------------------------------- /NSObjectIntrospectionDemo/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // NSObjectIntrospectionDemo 4 | // 5 | // Created by confidence on 21/12/2012. 6 | // Copyright (c) 2012 confidence. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @class ViewController; 12 | 13 | @interface AppDelegate : UIResponder 14 | 15 | @property (strong, nonatomic) UIWindow *window; 16 | 17 | @property (strong, nonatomic) ViewController *viewController; 18 | 19 | @end 20 | -------------------------------------------------------------------------------- /NSObjectIntrospectionDemo/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // NSObjectIntrospectionDemo 4 | // 5 | // Created by confidence on 21/12/2012. 6 | // Copyright (c) 2012 confidence. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | 11 | #import "ViewController.h" 12 | 13 | @implementation AppDelegate 14 | 15 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 16 | { 17 | self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 18 | // Override point for customization after application launch. 19 | self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; 20 | self.window.rootViewController = self.viewController; 21 | [self.window makeKeyAndVisible]; 22 | return YES; 23 | } 24 | 25 | - (void)applicationWillResignActive:(UIApplication *)application 26 | { 27 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 28 | // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 29 | } 30 | 31 | - (void)applicationDidEnterBackground:(UIApplication *)application 32 | { 33 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 34 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 35 | } 36 | 37 | - (void)applicationWillEnterForeground:(UIApplication *)application 38 | { 39 | // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 40 | } 41 | 42 | - (void)applicationDidBecomeActive:(UIApplication *)application 43 | { 44 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 45 | } 46 | 47 | - (void)applicationWillTerminate:(UIApplication *)application 48 | { 49 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 50 | } 51 | 52 | @end 53 | -------------------------------------------------------------------------------- /NSObjectIntrospectionDemo/Default-568h@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Daniel1of1/NSObjectIntrospection/e501a5d4e73b43164513491fb6b810d5f8c358c6/NSObjectIntrospectionDemo/Default-568h@2x.png -------------------------------------------------------------------------------- /NSObjectIntrospectionDemo/Default.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Daniel1of1/NSObjectIntrospection/e501a5d4e73b43164513491fb6b810d5f8c358c6/NSObjectIntrospectionDemo/Default.png -------------------------------------------------------------------------------- /NSObjectIntrospectionDemo/Default@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Daniel1of1/NSObjectIntrospection/e501a5d4e73b43164513491fb6b810d5f8c358c6/NSObjectIntrospectionDemo/Default@2x.png -------------------------------------------------------------------------------- /NSObjectIntrospectionDemo/DemoLeafObject.h: -------------------------------------------------------------------------------- 1 | // 2 | // DemoLeafObject.h 3 | // NSObjectIntrospectionDemo 4 | // 5 | // Created by confidence on 22/12/2012. 6 | // Copyright (c) 2012 confidence. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface DemoLeafObject : NSObject 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /NSObjectIntrospectionDemo/DemoLeafObject.m: -------------------------------------------------------------------------------- 1 | // 2 | // DemoLeafObject.m 3 | // NSObjectIntrospectionDemo 4 | // 5 | // Created by confidence on 22/12/2012. 6 | // Copyright (c) 2012 confidence. All rights reserved. 7 | // 8 | 9 | #import "DemoLeafObject.h" 10 | 11 | @implementation DemoLeafObject 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /NSObjectIntrospectionDemo/DemoObject.h: -------------------------------------------------------------------------------- 1 | // 2 | // DemoObject.h 3 | // NSObjectIntrospectionDemo 4 | // 5 | // Created by confidence on 22/12/2012. 6 | // Copyright (c) 2012 confidence. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "DemoLeafObject.h" 11 | 12 | @interface DemoObject : NSObject { 13 | NSString *anIvarString; 14 | } 15 | 16 | @property (nonatomic, strong) NSString *aString; 17 | @property (nonatomic, strong) NSNumber *aNumber; 18 | @property (nonatomic) CGFloat aFloat; 19 | @property (nonatomic, strong) DemoLeafObject *aLeafObject; 20 | @property (nonatomic, strong) NSArray *arrayOfLeaves; 21 | @property (nonatomic, retain) NSDictionary *dictOfLeaves; 22 | 23 | -(void)aDeclaredMethod; 24 | 25 | @end 26 | -------------------------------------------------------------------------------- /NSObjectIntrospectionDemo/DemoObject.m: -------------------------------------------------------------------------------- 1 | // 2 | // DemoObject.m 3 | // NSObjectIntrospectionDemo 4 | // 5 | // Created by confidence on 22/12/2012. 6 | // Copyright (c) 2012 confidence. All rights reserved. 7 | // 8 | 9 | #import "DemoObject.h" 10 | 11 | @implementation DemoObject 12 | 13 | @synthesize aString; 14 | @synthesize aNumber; 15 | @synthesize aFloat; 16 | @synthesize aLeafObject; 17 | @synthesize arrayOfLeaves; 18 | @synthesize dictOfLeaves; 19 | 20 | -(id)init{ 21 | 22 | if (self=[super init]) { 23 | anIvarString=@"anIvarString"; 24 | } 25 | 26 | return self; 27 | } 28 | 29 | -(void)aDeclaredMethod{ 30 | NSLog(@"aDeclaredMethod was called"); 31 | } 32 | 33 | -(void)anUnDeclaredMethod{ 34 | NSLog(@"anUnDeclaredMethod was called"); 35 | } 36 | 37 | @end 38 | -------------------------------------------------------------------------------- /NSObjectIntrospectionDemo/NSObjectIntrospectionDemo-Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleDisplayName 8 | ${PRODUCT_NAME} 9 | CFBundleExecutable 10 | ${EXECUTABLE_NAME} 11 | CFBundleIdentifier 12 | com.confidence.${PRODUCT_NAME:rfc1034identifier} 13 | CFBundleInfoDictionaryVersion 14 | 6.0 15 | CFBundleName 16 | ${PRODUCT_NAME} 17 | CFBundlePackageType 18 | APPL 19 | CFBundleShortVersionString 20 | 1.0 21 | CFBundleSignature 22 | ???? 23 | CFBundleVersion 24 | 1.0 25 | LSRequiresIPhoneOS 26 | 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /NSObjectIntrospectionDemo/NSObjectIntrospectionDemo-Prefix.pch: -------------------------------------------------------------------------------- 1 | // 2 | // Prefix header for all source files of the 'NSObjectIntrospectionDemo' target in the 'NSObjectIntrospectionDemo' project 3 | // 4 | 5 | #import 6 | 7 | #ifndef __IPHONE_4_0 8 | #warning "This project uses features only available in iOS SDK 4.0 and later." 9 | #endif 10 | 11 | #ifdef __OBJC__ 12 | #import 13 | #import 14 | #endif 15 | -------------------------------------------------------------------------------- /NSObjectIntrospectionDemo/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // NSObjectIntrospectionDemo 4 | // 5 | // Created by confidence on 21/12/2012. 6 | // Copyright (c) 2012 confidence. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface ViewController : UIViewController 12 | 13 | @end 14 | -------------------------------------------------------------------------------- /NSObjectIntrospectionDemo/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // NSObjectIntrospectionDemo 4 | // 5 | // Created by confidence on 21/12/2012. 6 | // Copyright (c) 2012 confidence. All rights reserved. 7 | // 8 | 9 | #import "ViewController.h" 10 | 11 | @interface ViewController () 12 | 13 | @end 14 | 15 | @implementation ViewController 16 | 17 | - (void)viewDidLoad 18 | { 19 | [super viewDidLoad]; 20 | // Do any additional setup after loading the view, typically from a nib. 21 | } 22 | 23 | - (void)didReceiveMemoryWarning 24 | { 25 | [super didReceiveMemoryWarning]; 26 | // Dispose of any resources that can be recreated. 27 | } 28 | 29 | @end 30 | -------------------------------------------------------------------------------- /NSObjectIntrospectionDemo/en.lproj/InfoPlist.strings: -------------------------------------------------------------------------------- 1 | /* Localized versions of Info.plist keys */ 2 | 3 | -------------------------------------------------------------------------------- /NSObjectIntrospectionDemo/en.lproj/ViewController.xib: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 1536 5 | 12A269 6 | 2835 7 | 1187 8 | 624.00 9 | 10 | com.apple.InterfaceBuilder.IBCocoaTouchPlugin 11 | 1919 12 | 13 | 14 | IBProxyObject 15 | IBUIView 16 | 17 | 18 | com.apple.InterfaceBuilder.IBCocoaTouchPlugin 19 | 20 | 21 | PluginDependencyRecalculationVersion 22 | 23 | 24 | 25 | 26 | IBFilesOwner 27 | IBCocoaTouchFramework 28 | 29 | 30 | IBFirstResponder 31 | IBCocoaTouchFramework 32 | 33 | 34 | 35 | 274 36 | {{0, 20}, {320, 548}} 37 | 38 | 39 | 40 | 41 | 3 42 | MC43NQA 43 | 44 | 2 45 | 46 | 47 | NO 48 | 49 | 50 | IBUIScreenMetrics 51 | 52 | YES 53 | 54 | 55 | 56 | 57 | 58 | {320, 568} 59 | {568, 320} 60 | 61 | 62 | IBCocoaTouchFramework 63 | Retina 4 Full Screen 64 | 2 65 | 66 | IBCocoaTouchFramework 67 | 68 | 69 | 70 | 71 | 72 | 73 | view 74 | 75 | 76 | 77 | 7 78 | 79 | 80 | 81 | 82 | 83 | 0 84 | 85 | 86 | 87 | 88 | 89 | -1 90 | 91 | 92 | File's Owner 93 | 94 | 95 | -2 96 | 97 | 98 | 99 | 100 | 6 101 | 102 | 103 | 104 | 105 | 106 | 107 | ViewController 108 | com.apple.InterfaceBuilder.IBCocoaTouchPlugin 109 | UIResponder 110 | com.apple.InterfaceBuilder.IBCocoaTouchPlugin 111 | com.apple.InterfaceBuilder.IBCocoaTouchPlugin 112 | 113 | 114 | 115 | 116 | 117 | 7 118 | 119 | 120 | 121 | 122 | ViewController 123 | UIViewController 124 | 125 | IBProjectSource 126 | ./Classes/ViewController.h 127 | 128 | 129 | 130 | 131 | 0 132 | IBCocoaTouchFramework 133 | YES 134 | 3 135 | YES 136 | 1919 137 | 138 | 139 | -------------------------------------------------------------------------------- /NSObjectIntrospectionDemo/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // NSObjectIntrospectionDemo 4 | // 5 | // Created by confidence on 21/12/2012. 6 | // Copyright (c) 2012 confidence. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | #import "AppDelegate.h" 12 | 13 | int main(int argc, char *argv[]) 14 | { 15 | @autoreleasepool { 16 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /NSObjectIntrospectionDemoTests/NSObject+IntrospectTests.h: -------------------------------------------------------------------------------- 1 | // 2 | // NSObject+IntrospectTests.h 3 | // NSObjectIntrospectionDemo 4 | // 5 | // Created by confidence on 22/12/2012. 6 | // Copyright (c) 2012 confidence. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "DemoObject.h" 11 | #import "DemoLeafObject.h" 12 | #import "NSObject+introspect.h" 13 | 14 | @interface NSObject_IntrospectTests : SenTestCase 15 | 16 | @property(nonatomic, strong) DemoObject *demoObj; 17 | @property(nonatomic, strong) DemoLeafObject *demoLeafObj; 18 | 19 | @end 20 | -------------------------------------------------------------------------------- /NSObjectIntrospectionDemoTests/NSObject+IntrospectTests.m: -------------------------------------------------------------------------------- 1 | // 2 | // NSObject+IntrospectTests.m 3 | // NSObjectIntrospectionDemo 4 | // 5 | // Created by confidence on 22/12/2012. 6 | // Copyright (c) 2012 confidence. All rights reserved. 7 | // 8 | 9 | #import "NSObject+IntrospectTests.h" 10 | 11 | @implementation NSObject_IntrospectTests 12 | 13 | @synthesize demoObj; 14 | @synthesize demoLeafObj; 15 | 16 | - (void)setUp 17 | { 18 | [super setUp]; 19 | 20 | self.demoObj=[[DemoObject alloc] init]; 21 | self.demoLeafObj=[[DemoLeafObject alloc] init]; 22 | } 23 | 24 | - (void)tearDown 25 | { 26 | self.demoObj=nil; 27 | self.demoLeafObj=nil; 28 | 29 | [super tearDown]; 30 | } 31 | 32 | -(void)testPropertiesDictReturnsCorrectDictionary{ 33 | 34 | demoObj.aString=@"some string value"; 35 | demoObj.aFloat=8; 36 | demoObj.aNumber=[NSNumber numberWithDouble:M_PI]; 37 | demoObj.aLeafObject=[[DemoLeafObject alloc] init]; 38 | DemoLeafObject *leaf1=[[DemoLeafObject alloc] init]; 39 | DemoLeafObject *leaf2=[[DemoLeafObject alloc] init]; 40 | DemoLeafObject *leaf3=[[DemoLeafObject alloc] init]; 41 | demoObj.arrayOfLeaves=[NSArray arrayWithObjects:leaf1, leaf2, leaf3,@"another string value", nil]; 42 | demoObj.dictOfLeaves=[NSDictionary dictionaryWithObjects:demoObj.arrayOfLeaves forKeys:[NSArray arrayWithObjects:@"key1",@"key2",@"key3",@"key4", nil]]; 43 | 44 | NSDictionary *propsDict=[demoObj propertiesDict]; 45 | 46 | NSDictionary *correctDictionary=@{ 47 | @"aString" : [demoObj valueForKey:@"aString"], 48 | @"aNumber" : [demoObj valueForKey:@"aNumber"], 49 | @"aFloat" : [demoObj valueForKey:@"aFloat"], 50 | @"aLeafObject" : [demoObj valueForKey:@"aLeafObject"], 51 | @"arrayOfLeaves" : [demoObj valueForKey:@"arrayOfLeaves"], 52 | @"dictOfLeaves" : [demoObj valueForKey:@"dictOfLeaves"] 53 | }; 54 | 55 | STAssertTrue([propsDict isEqualToDictionary:correctDictionary] , @"propertiesDict should return correct dictionary it looks like this %@\n it should be this %@",propsDict.description,correctDictionary.description); 56 | 57 | } 58 | 59 | 60 | -(void)testIVarsDictReturnsCorrectDictionary{ 61 | demoObj.aString=@"some string value"; 62 | demoObj.aFloat=8; 63 | demoObj.aNumber=[NSNumber numberWithDouble:M_PI]; 64 | demoObj.aLeafObject=[[DemoLeafObject alloc] init]; 65 | DemoLeafObject *leaf1=[[DemoLeafObject alloc] init]; 66 | DemoLeafObject *leaf2=[[DemoLeafObject alloc] init]; 67 | DemoLeafObject *leaf3=[[DemoLeafObject alloc] init]; 68 | demoObj.arrayOfLeaves=[NSArray arrayWithObjects:leaf1, leaf2, leaf3,@"another string value", nil]; 69 | demoObj.dictOfLeaves=[NSDictionary dictionaryWithObjects:demoObj.arrayOfLeaves forKeys:[NSArray arrayWithObjects:@"key1",@"key2",@"key3",@"key4", nil]]; 70 | 71 | NSDictionary *iVarsDict=[demoObj iVarsDict]; 72 | 73 | NSDictionary *correctDictionary=@{ 74 | @"anIvarString" : [demoObj valueForKey:@"anIvarString"], 75 | @"aString" : [demoObj valueForKey:@"aString"], 76 | @"aNumber" : [demoObj valueForKey:@"aNumber"], 77 | @"aFloat" : [demoObj valueForKey:@"aFloat"], 78 | @"aLeafObject" : [demoObj valueForKey:@"aLeafObject"], 79 | @"arrayOfLeaves" : [demoObj valueForKey:@"arrayOfLeaves"], 80 | @"dictOfLeaves" : [demoObj valueForKey:@"dictOfLeaves"] 81 | }; 82 | 83 | STAssertTrue([iVarsDict isEqualToDictionary:correctDictionary] , @"iVarsDict should return correct dictionary it looks like this %@\n it should be this %@",iVarsDict.description,correctDictionary.description); 84 | 85 | } 86 | 87 | -(void)testMethodsDictReturnsCorrectDictionary{ 88 | 89 | NSDictionary *methodsDict=[demoObj methodsDict]; 90 | 91 | NSDictionary *correctDictionary=@{ 92 | //@".cxx_destruct" : @".cxx_destruct", 93 | @"aDeclaredMethod" : @"aDeclaredMethod", 94 | @"aFloat" : @"aFloat", 95 | @"aLeafObject" : @"aLeafObject", 96 | @"aNumber" : @"aNumber", 97 | @"aString" : @"aString", 98 | @"anUnDeclaredMethod" : @"anUnDeclaredMethod", 99 | @"arrayOfLeaves" : @"arrayOfLeaves", 100 | @"dictOfLeaves" : @"dictOfLeaves", 101 | @"init" : @"init", 102 | @"setAFloat:" : @"setAFloat:", 103 | @"setALeafObject:" : @"setALeafObject:", 104 | @"setANumber:" : @"setANumber:", 105 | @"setAString:" : @"setAString:", 106 | @"setArrayOfLeaves:" : @"setArrayOfLeaves:", 107 | @"setDictOfLeaves:" : @"setDictOfLeaves:", 108 | }; 109 | 110 | STAssertTrue([methodsDict isEqualToDictionary:correctDictionary], @"methodsDict should return correct dictionary it looks like this %@\n it should be this %@",methodsDict.description,correctDictionary.description); 111 | 112 | } 113 | 114 | -(void)testObjectIntrospectDictionaryReturnsCorrectDictionary{ 115 | demoObj.aString=@"some string value"; 116 | demoObj.aFloat=8; 117 | demoObj.aNumber=[NSNumber numberWithDouble:M_PI]; 118 | demoObj.aLeafObject=[[DemoLeafObject alloc] init]; 119 | DemoLeafObject *leaf1=[[DemoLeafObject alloc] init]; 120 | DemoLeafObject *leaf2=[[DemoLeafObject alloc] init]; 121 | DemoLeafObject *leaf3=[[DemoLeafObject alloc] init]; 122 | demoObj.arrayOfLeaves=[NSArray arrayWithObjects:leaf1, leaf2, leaf3,@"another string value", nil]; 123 | demoObj.dictOfLeaves=[NSDictionary dictionaryWithObjects:demoObj.arrayOfLeaves forKeys:[NSArray arrayWithObjects:@"key1",@"key2",@"key3",@"key4", nil]]; 124 | 125 | NSDictionary *correctPropsDictionary=@{ 126 | @"aString" : [demoObj valueForKey:@"aString"], 127 | @"aNumber" : [demoObj valueForKey:@"aNumber"], 128 | @"aFloat" : [demoObj valueForKey:@"aFloat"], 129 | @"aLeafObject" : [demoObj valueForKey:@"aLeafObject"], 130 | @"arrayOfLeaves" : [demoObj valueForKey:@"arrayOfLeaves"], 131 | @"dictOfLeaves" : [demoObj valueForKey:@"dictOfLeaves"] 132 | }; 133 | 134 | NSDictionary *correcIVarstDictionary=@{ 135 | @"anIvarString" : [demoObj valueForKey:@"anIvarString"], 136 | @"aString" : [demoObj valueForKey:@"aString"], 137 | @"aNumber" : [demoObj valueForKey:@"aNumber"], 138 | @"aFloat" : [demoObj valueForKey:@"aFloat"], 139 | @"aLeafObject" : [demoObj valueForKey:@"aLeafObject"], 140 | @"arrayOfLeaves" : [demoObj valueForKey:@"arrayOfLeaves"], 141 | @"dictOfLeaves" : [demoObj valueForKey:@"dictOfLeaves"] 142 | }; 143 | 144 | NSDictionary *correctMethodsDictionary=@{ 145 | //@".cxx_destruct" : @".cxx_destruct", 146 | @"aDeclaredMethod" : @"aDeclaredMethod", 147 | @"aFloat" : @"aFloat", 148 | @"aLeafObject" : @"aLeafObject", 149 | @"aNumber" : @"aNumber", 150 | @"aString" : @"aString", 151 | @"anUnDeclaredMethod" : @"anUnDeclaredMethod", 152 | @"arrayOfLeaves" : @"arrayOfLeaves", 153 | @"dictOfLeaves" : @"dictOfLeaves", 154 | @"init" : @"init", 155 | @"setAFloat:" : @"setAFloat:", 156 | @"setALeafObject:" : @"setALeafObject:", 157 | @"setANumber:" : @"setANumber:", 158 | @"setAString:" : @"setAString:", 159 | @"setArrayOfLeaves:" : @"setArrayOfLeaves:", 160 | @"setDictOfLeaves:" : @"setDictOfLeaves:", 161 | }; 162 | 163 | 164 | NSArray *keys=[[NSArray alloc] initWithObjects:@"properties",@"iVars",@"methods",nil]; 165 | 166 | NSDictionary *objectIntrospectDict=[demoObj objectIntrospectDictionary]; 167 | 168 | NSDictionary *correctDictionary=[NSDictionary dictionaryWithObjects:@[correctPropsDictionary, correcIVarstDictionary,correctMethodsDictionary] forKeys:keys]; 169 | 170 | STAssertTrue([objectIntrospectDict isEqualToDictionary:correctDictionary], @"objectIntrospectDict should return correct dictionary it looks like this %@\n it should be this %@",objectIntrospectDict.description,correctDictionary.description); 171 | 172 | } 173 | 174 | @end 175 | -------------------------------------------------------------------------------- /NSObjectIntrospectionDemoTests/NSObjectIntrospectionDemoTests-Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | ${EXECUTABLE_NAME} 9 | CFBundleIdentifier 10 | com.confidence.${PRODUCT_NAME:rfc1034identifier} 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundlePackageType 14 | BNDL 15 | CFBundleShortVersionString 16 | 1.0 17 | CFBundleSignature 18 | ???? 19 | CFBundleVersion 20 | 1 21 | 22 | 23 | -------------------------------------------------------------------------------- /NSObjectIntrospectionDemoTests/en.lproj/InfoPlist.strings: -------------------------------------------------------------------------------- 1 | /* Localized versions of Info.plist keys */ 2 | 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | NSObjectIntrospection 2 | ===================== 3 | 4 | A category on NSObject to allow for runtime introspection. 5 | It allows you to generate a dictionary with key value pairs of an objects properties. 6 | 7 | ## Prerequisites 8 | - These exensions use ARC; Hence ios4.0+ 9 | 10 | ## How to install 11 | 12 | - Drag NSObject+introspection folder into your project. 13 | - `#import "NSObject+introspection.h"` wherever needed. 14 | 15 | 16 | ## Usage 17 | 18 | Since NSObject is the root class of almost any Objective-C object, it can be called by almost any class you have. 19 | 20 | It exposes 4 methods: 21 | 22 | ```objective-c 23 | -(NSDictionary*)dhc_propertiesDict; //returns a dictionary with key-value pairs of the objects declared properties 24 | 25 | -(NSDictionary *)dhc_iVarsDict; //returns a dictionary with key-value pairs of the objects iVars 26 | 27 | -(NSDictionary *)dhc_methodsDict; //returns a dictionary with key-value pairs of the objects methods 28 | //currently the key and value are both the method name string 29 | 30 | -(NSDictionary *)dhc_objectIntrospectDictionary; //returns a dictionary with the above 3 dictionaries with keys "properties", "iVars" and "methods" 31 | 32 | ``` 33 | 34 | ### Usage notes 35 | - any property or iVar that is `nil` will be an `[NSNull null]` value for it's corresponding key. 36 | - any scalar properties will automatically be converted to / from their objective equivalents. 37 | 38 | ## Example 39 | SomeObject.h 40 | 41 | ``` 42 | //SomeObject.h 43 | #import 44 | #import "SomeOtherObject.h" // not described in readme 45 | 46 | @interface SomeObject : NSObject { 47 | NSString *anIvarString; 48 | } 49 | 50 | @property (nonatomic, strong) NSString *someString; 51 | @property (nonatomic, strong) SomeOtherObject *someOtherObject; 52 | ``` 53 | 54 | SomeObject.m 55 | 56 | ``` 57 | //SomeObject.m 58 | #import "SomeObject.h" 59 | 60 | @implementation SomeObject 61 | 62 | @synthesize someString; 63 | @synthesize someOtherObject; 64 | 65 | -(id)init{ 66 | self=[super init]; 67 | 68 | if(self){ 69 | anIvarString=@"some string value"; 70 | self.someString=@"a string which is a property of our object" 71 | self.someOtherObject=[[SomeOtherObject alloc] init]; 72 | } 73 | 74 | return self; 75 | } 76 | 77 | ``` 78 | 79 | anywhere in your code 80 | 81 | ``` 82 | #import "NSObject+Introspection.h" 83 | #import "SomeObject.h" 84 | ... 85 | SomeObject *someObject=[[SomeObject alloc] init]; 86 | 87 | NSDictionary *propsDict=[someObject propertiesDict]; 88 | 89 | NSLog (@"%@",propsDict.description); 90 | ``` 91 | 92 | log output 93 | 94 | ``` 95 | { 96 | someOtherObject = ""; //the dictionary contains the pointer to the actual instance 97 | someString = "a string which is a property of our object"; 98 | } 99 | 100 | ``` 101 | 102 | 103 | ### todo 104 | - talk about removal of .cxx_desctruct method from methods dict, its a method on every nsobject called after dealloc 105 | - add some more interesting extensions :) 106 | --------------------------------------------------------------------------------