Advertisement
sophtwhere

NSObject+classSwitch.h

Oct 29th, 2013
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  NSObject+classSwitch.h
  3. //
  4. //  Created by Jonathan Annett on 30/10/13.
  5. //  Copyright (c) 2013 Sophtwhere. All rights reserved.
  6. //
  7. /*
  8.  
  9.  
  10.  example 1: // simple c style switch/case based on instantiated class type.
  11.  
  12.  
  13.         switch (testme.classSwitchCode) {
  14.             case isNSString:
  15.                 NSLog(@"%@ is a string!",testme);
  16.                 break;
  17.             case isNSArray:
  18.                 NSLog(@"%@ is an array with %d elements!",testme,[(NSArray*) testme count]);
  19.                
  20.                 break;
  21.             case isNSDictionary:
  22.                
  23.                 NSLog(@"%@ is an dictionary with these keys: %@!",testme,[(NSDictionary*) testme allKeys]);
  24.                
  25.                 break;
  26.                
  27.             default:
  28.                 break;
  29.         }
  30.  
  31.  
  32.  example 2: define blocks in a dictionary and dispatch the appropriate one
  33.  
  34.     NSDictionary *checkerBlocks = @{
  35.                                    
  36.                                     _isUndefinedObject_ : ^{
  37.                                        
  38.                                         NSLog(@"don't know what to do with this");
  39.  
  40.                                     },
  41.                                    
  42.                                     // use same handler for NSDictionary and NSMutableDictionary
  43.                                    _isNSMutableDictionary_ : _isNSDictionary_,
  44.                                    _isNSDictionary_ :
  45.                                        ^{
  46.                                            NSLog(@"this is a block for a dictionary");
  47.                                        },
  48.                                    
  49.                                     // treat NSURL as undefined.
  50.                                     _isNSURL_ : _isUndefinedObject_,
  51.                                    
  52.                                    _isNSArray_ :
  53.                                        ^{
  54.                                            NSLog(@"this is a block for an array");
  55.                                        },
  56.                                    
  57.                                    _isNSString_ :
  58.                                        ^{
  59.                                            NSLog(@"this is a block for a string - success");
  60.                                        }
  61.                                    
  62.                                    };
  63.    
  64.                                        
  65.                                             }];
  66.  
  67.      [object1 class_specific_block_dispatch_async:dispatch_get_main_queue() blocks:checkerBlocks];
  68.      [object2 class_specific_block_dispatch_async:dispatch_get_main_queue() blocks:checkerBlocks];
  69.      [object3 class_specific_block_dispatch_async:dispatch_get_main_queue() blocks:checkerBlocks];
  70.  
  71.  
  72. */
  73.  
  74. #import <Foundation/Foundation.h>
  75. #import <AVFoundation/AVFoundation.h>
  76. #import <AssetsLibrary/AssetsLibrary.h>
  77. #import <MediaPlayer/MediaPlayer.h>
  78.  
  79.  
  80.  
  81. #define DEFINE_OBECTS()\
  82. DEF_OBJ_CODE(NSString)\
  83. DEF_OBJ_CODE(NSArray)\
  84. DEF_OBJ_CODE(NSMutableArray)\
  85. DEF_OBJ_CODE(NSDictionary)\
  86. DEF_OBJ_CODE(NSMutableDictionary)\
  87. DEF_OBJ_CODE(AVURLAsset)\
  88. DEF_OBJ_CODE(ALAsset)\
  89. DEF_OBJ_CODE(NSURL)\
  90. DEF_OBJ_CODE(UIImage)\
  91. DEF_OBJ_CODE(MPMediaItem)\
  92. DEF_OBJ_CODE(NSURLConnection)\
  93.  
  94.  
  95.  
  96.  
  97.  
  98. /////////////////////////////////
  99. #define DEF_OBJ_CODE(NSObject)\
  100. extern NSString *_is##NSObject##_;
  101. DEFINE_OBECTS()
  102. #undef DEF_OBJ_CODE
  103. /////////////////////////////////
  104. extern NSString *_isUndefinedObject_;
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114. /////////////////////////////////
  115. #define DEF_OBJ_CODE(NSObject)\
  116.   is##NSObject,
  117.  
  118. enum nsObjectSwitchCode {
  119.     isUndefinedObject=0,
  120.     DEFINE_OBECTS()
  121. };
  122.  
  123. #undef DEF_OBJ_CODE
  124. /////////////////////////////////
  125.  
  126.  
  127. /////////////////////////////////
  128. #define DEF_OBJ_CODE(NSObject)\
  129. @interface NSObject (classSwitch)\
  130. -(enum nsObjectSwitchCode) classSwitchCode;\
  131. +(enum nsObjectSwitchCode) classSwitchCode;\
  132. -(NSObject*) as##NSObject;\
  133. -(BOOL) is##NSObject;\
  134. @end
  135.  
  136. DEFINE_OBECTS()
  137.  
  138. #undef DEF_OBJ_CODE
  139. /////////////////////////////////
  140.  
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151. /////////////////////////////////
  152. #define DEF_OBJ_CODE(NSObject)\
  153. -(BOOL) is##NSObject;\
  154. -(NSObject*) as##NSObject;
  155.  
  156. @interface NSObject(classSwitchDefaults)
  157. -(dispatch_block_t) class_specific_block_copy:(NSDictionary*) blocks;
  158. -(void) class_specific_block_dispatch:(NSDictionary*) blocks;
  159. -(void) class_specific_block_dispatch_async:(dispatch_queue_t) queue
  160.                                      blocks:(NSDictionary*) blocks;
  161. -(enum nsObjectSwitchCode) classSwitchCode ;
  162. -(BOOL)isUndefinedObject ;
  163.  
  164. DEFINE_OBECTS()
  165.  
  166. @end
  167. #undef DEF_OBJ_CODE
  168. /////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement