FLEXRuntimeUtility.m 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. //
  2. // FLEXRuntimeUtility.m
  3. // Flipboard
  4. //
  5. // Created by Ryan Olson on 6/8/14.
  6. // Copyright (c) 2014 Flipboard. All rights reserved.
  7. //
  8. #import <UIKit/UIKit.h>
  9. #import "FLEXRuntimeUtility.h"
  10. // See https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtPropertyIntrospection.html#//apple_ref/doc/uid/TP40008048-CH101-SW6
  11. NSString *const kFLEXUtilityAttributeTypeEncoding = @"T";
  12. NSString *const kFLEXUtilityAttributeBackingIvar = @"V";
  13. NSString *const kFLEXUtilityAttributeReadOnly = @"R";
  14. NSString *const kFLEXUtilityAttributeCopy = @"C";
  15. NSString *const kFLEXUtilityAttributeRetain = @"&";
  16. NSString *const kFLEXUtilityAttributeNonAtomic = @"N";
  17. NSString *const kFLEXUtilityAttributeCustomGetter = @"G";
  18. NSString *const kFLEXUtilityAttributeCustomSetter = @"S";
  19. NSString *const kFLEXUtilityAttributeDynamic = @"D";
  20. NSString *const kFLEXUtilityAttributeWeak = @"W";
  21. NSString *const kFLEXUtilityAttributeGarbageCollectable = @"P";
  22. NSString *const kFLEXUtilityAttributeOldStyleTypeEncoding = @"t";
  23. static NSString *const FLEXRuntimeUtilityErrorDomain = @"FLEXRuntimeUtilityErrorDomain";
  24. typedef NS_ENUM(NSInteger, FLEXRuntimeUtilityErrorCode) {
  25. FLEXRuntimeUtilityErrorCodeDoesNotRecognizeSelector = 0,
  26. FLEXRuntimeUtilityErrorCodeInvocationFailed = 1,
  27. FLEXRuntimeUtilityErrorCodeArgumentTypeMismatch = 2
  28. };
  29. // Arguments 0 and 1 are self and _cmd always
  30. const unsigned int kFLEXNumberOfImplicitArgs = 2;
  31. @implementation FLEXRuntimeUtility
  32. #pragma mark - Property Helpers (Public)
  33. + (NSString *)prettyNameForProperty:(objc_property_t)property
  34. {
  35. NSString *name = @(property_getName(property));
  36. NSString *encoding = [self typeEncodingForProperty:property];
  37. NSString *readableType = [self readableTypeForEncoding:encoding];
  38. return [self appendName:name toType:readableType];
  39. }
  40. + (NSString *)typeEncodingForProperty:(objc_property_t)property
  41. {
  42. NSDictionary *attributesDictionary = [self attributesDictionaryForProperty:property];
  43. return attributesDictionary[kFLEXUtilityAttributeTypeEncoding];
  44. }
  45. + (BOOL)isReadonlyProperty:(objc_property_t)property
  46. {
  47. return [[self attributesDictionaryForProperty:property] objectForKey:kFLEXUtilityAttributeReadOnly] != nil;
  48. }
  49. + (SEL)setterSelectorForProperty:(objc_property_t)property
  50. {
  51. SEL setterSelector = NULL;
  52. NSString *setterSelectorString = [[self attributesDictionaryForProperty:property] objectForKey:kFLEXUtilityAttributeCustomSetter];
  53. if (!setterSelectorString) {
  54. NSString *propertyName = @(property_getName(property));
  55. setterSelectorString = [NSString stringWithFormat:@"set%@%@:", [[propertyName substringToIndex:1] uppercaseString], [propertyName substringFromIndex:1]];
  56. }
  57. if (setterSelectorString) {
  58. setterSelector = NSSelectorFromString(setterSelectorString);
  59. }
  60. return setterSelector;
  61. }
  62. + (NSString *)fullDescriptionForProperty:(objc_property_t)property
  63. {
  64. NSDictionary *attributesDictionary = [self attributesDictionaryForProperty:property];
  65. NSMutableArray *attributesStrings = [NSMutableArray array];
  66. // Atomicity
  67. if (attributesDictionary[kFLEXUtilityAttributeNonAtomic]) {
  68. [attributesStrings addObject:@"nonatomic"];
  69. } else {
  70. [attributesStrings addObject:@"atomic"];
  71. }
  72. // Storage
  73. if (attributesDictionary[kFLEXUtilityAttributeRetain]) {
  74. [attributesStrings addObject:@"strong"];
  75. } else if (attributesDictionary[kFLEXUtilityAttributeCopy]) {
  76. [attributesStrings addObject:@"copy"];
  77. } else if (attributesDictionary[kFLEXUtilityAttributeWeak]) {
  78. [attributesStrings addObject:@"weak"];
  79. } else {
  80. [attributesStrings addObject:@"assign"];
  81. }
  82. // Mutability
  83. if (attributesDictionary[kFLEXUtilityAttributeReadOnly]) {
  84. [attributesStrings addObject:@"readonly"];
  85. } else {
  86. [attributesStrings addObject:@"readwrite"];
  87. }
  88. // Custom getter/setter
  89. NSString *customGetter = attributesDictionary[kFLEXUtilityAttributeCustomGetter];
  90. NSString *customSetter = attributesDictionary[kFLEXUtilityAttributeCustomSetter];
  91. if (customGetter) {
  92. [attributesStrings addObject:[NSString stringWithFormat:@"getter=%@", customGetter]];
  93. }
  94. if (customSetter) {
  95. [attributesStrings addObject:[NSString stringWithFormat:@"setter=%@", customSetter]];
  96. }
  97. NSString *attributesString = [attributesStrings componentsJoinedByString:@", "];
  98. NSString *shortName = [self prettyNameForProperty:property];
  99. return [NSString stringWithFormat:@"@property (%@) %@", attributesString, shortName];
  100. }
  101. + (id)valueForProperty:(objc_property_t)property onObject:(id)object
  102. {
  103. NSString *customGetterString = nil;
  104. char *customGetterName = property_copyAttributeValue(property, "G");
  105. if (customGetterName) {
  106. customGetterString = @(customGetterName);
  107. free(customGetterName);
  108. }
  109. SEL getterSelector;
  110. if ([customGetterString length] > 0) {
  111. getterSelector = NSSelectorFromString(customGetterString);
  112. } else {
  113. NSString *propertyName = @(property_getName(property));
  114. getterSelector = NSSelectorFromString(propertyName);
  115. }
  116. return [self performSelector:getterSelector onObject:object withArguments:nil error:NULL];
  117. }
  118. + (NSString *)descriptionForIvarOrPropertyValue:(id)value
  119. {
  120. NSString *description = nil;
  121. // Special case BOOL for better readability.
  122. if ([value isKindOfClass:[NSValue class]]) {
  123. const char *type = [value objCType];
  124. if (strcmp(type, @encode(BOOL)) == 0) {
  125. BOOL boolValue = NO;
  126. [value getValue:&boolValue];
  127. description = boolValue ? @"YES" : @"NO";
  128. } else if (strcmp(type, @encode(SEL)) == 0) {
  129. SEL selector = NULL;
  130. [value getValue:&selector];
  131. description = NSStringFromSelector(selector);
  132. }
  133. }
  134. if (!description) {
  135. // Single line display - replace newlines and tabs with spaces.
  136. description = [[value description] stringByReplacingOccurrencesOfString:@"\n" withString:@" "];
  137. description = [description stringByReplacingOccurrencesOfString:@"\t" withString:@" "];
  138. }
  139. if (!description) {
  140. description = @"nil";
  141. }
  142. return description;
  143. }
  144. + (void)tryAddPropertyWithName:(const char *)name attributes:(NSDictionary *)attributePairs toClass:(__unsafe_unretained Class)theClass
  145. {
  146. objc_property_t property = class_getProperty(theClass, name);
  147. if (!property) {
  148. unsigned int totalAttributesCount = (unsigned int)[attributePairs count];
  149. objc_property_attribute_t *attributes = malloc(sizeof(objc_property_attribute_t) * totalAttributesCount);
  150. if (attributes) {
  151. unsigned int attributeIndex = 0;
  152. for (NSString *attributeName in [attributePairs allKeys]) {
  153. objc_property_attribute_t attribute;
  154. attribute.name = [attributeName UTF8String];
  155. attribute.value = [attributePairs[attributeName] UTF8String];
  156. attributes[attributeIndex++] = attribute;
  157. }
  158. class_addProperty(theClass, name, attributes, totalAttributesCount);
  159. free(attributes);
  160. }
  161. }
  162. }
  163. #pragma mark - Ivar Helpers (Public)
  164. + (NSString *)prettyNameForIvar:(Ivar)ivar
  165. {
  166. const char *nameCString = ivar_getName(ivar);
  167. NSString *name = nameCString ? @(nameCString) : nil;
  168. const char *encodingCString = ivar_getTypeEncoding(ivar);
  169. NSString *encoding = encodingCString ? @(encodingCString) : nil;
  170. NSString *readableType = [self readableTypeForEncoding:encoding];
  171. return [self appendName:name toType:readableType];
  172. }
  173. + (id)valueForIvar:(Ivar)ivar onObject:(id)object
  174. {
  175. id value = nil;
  176. const char *type = ivar_getTypeEncoding(ivar);
  177. #ifdef __arm64__
  178. // See http://www.sealiesoftware.com/blog/archive/2013/09/24/objc_explain_Non-pointer_isa.html
  179. const char *name = ivar_getName(ivar);
  180. if (type[0] == @encode(Class)[0] && strcmp(name, "isa") == 0) {
  181. value = object_getClass(object);
  182. } else
  183. #endif
  184. if (type[0] == @encode(id)[0] || type[0] == @encode(Class)[0]) {
  185. value = object_getIvar(object, ivar);
  186. } else {
  187. ptrdiff_t offset = ivar_getOffset(ivar);
  188. void *pointer = (__bridge void *)object + offset;
  189. value = [self valueForPrimitivePointer:pointer objCType:type];
  190. }
  191. return value;
  192. }
  193. + (void)setValue:(id)value forIvar:(Ivar)ivar onObject:(id)object
  194. {
  195. const char *typeEncodingCString = ivar_getTypeEncoding(ivar);
  196. if (typeEncodingCString[0] == '@') {
  197. object_setIvar(object, ivar, value);
  198. } else if ([value isKindOfClass:[NSValue class]]) {
  199. // Primitive - unbox the NSValue.
  200. NSValue *valueValue = (NSValue *)value;
  201. // Make sure that the box contained the correct type.
  202. NSAssert(strcmp([valueValue objCType], typeEncodingCString) == 0, @"Type encoding mismatch (value: %s; ivar: %s) in setting ivar named: %s on object: %@", [valueValue objCType], typeEncodingCString, ivar_getName(ivar), object);
  203. NSUInteger bufferSize = 0;
  204. @try {
  205. // NSGetSizeAndAlignment barfs on type encoding for bitfields.
  206. NSGetSizeAndAlignment(typeEncodingCString, &bufferSize, NULL);
  207. } @catch (NSException *exception) { }
  208. if (bufferSize > 0) {
  209. void *buffer = calloc(bufferSize, 1);
  210. [valueValue getValue:buffer];
  211. ptrdiff_t offset = ivar_getOffset(ivar);
  212. void *pointer = (__bridge void *)object + offset;
  213. memcpy(pointer, buffer, bufferSize);
  214. free(buffer);
  215. }
  216. }
  217. }
  218. #pragma mark - Method Helpers (Public)
  219. + (NSString *)prettyNameForMethod:(Method)method isClassMethod:(BOOL)isClassMethod
  220. {
  221. NSString *selectorName = NSStringFromSelector(method_getName(method));
  222. NSString *methodTypeString = isClassMethod ? @"+" : @"-";
  223. char *returnType = method_copyReturnType(method);
  224. NSString *readableReturnType = [self readableTypeForEncoding:@(returnType)];
  225. free(returnType);
  226. NSString *prettyName = [NSString stringWithFormat:@"%@ (%@)", methodTypeString, readableReturnType];
  227. NSArray *components = [self prettyArgumentComponentsForMethod:method];
  228. if ([components count] > 0) {
  229. prettyName = [prettyName stringByAppendingString:[components componentsJoinedByString:@" "]];
  230. } else {
  231. prettyName = [prettyName stringByAppendingString:selectorName];
  232. }
  233. return prettyName;
  234. }
  235. + (NSArray *)prettyArgumentComponentsForMethod:(Method)method
  236. {
  237. NSMutableArray *components = [NSMutableArray array];
  238. NSString *selectorName = NSStringFromSelector(method_getName(method));
  239. NSArray *selectorComponents = [selectorName componentsSeparatedByString:@":"];
  240. unsigned int numberOfArguments = method_getNumberOfArguments(method);
  241. for (unsigned int argIndex = kFLEXNumberOfImplicitArgs; argIndex < numberOfArguments; argIndex++) {
  242. char *argType = method_copyArgumentType(method, argIndex);
  243. NSString *readableArgType = [self readableTypeForEncoding:@(argType)];
  244. free(argType);
  245. NSString *prettyComponent = [NSString stringWithFormat:@"%@:(%@) ", [selectorComponents objectAtIndex:argIndex - kFLEXNumberOfImplicitArgs], readableArgType];
  246. [components addObject:prettyComponent];
  247. }
  248. return components;
  249. }
  250. #pragma mark - Method Calling/Field Editing (Public)
  251. + (id)performSelector:(SEL)selector onObject:(id)object withArguments:(NSArray *)arguments error:(NSError * __autoreleasing *)error
  252. {
  253. // Bail if the object won't respond to this selector.
  254. if (![object respondsToSelector:selector]) {
  255. if (error) {
  256. NSDictionary *userInfo = @{ NSLocalizedDescriptionKey : [NSString stringWithFormat:@"%@ does not respond to the selector %@", object, NSStringFromSelector(selector)]};
  257. *error = [NSError errorWithDomain:FLEXRuntimeUtilityErrorDomain code:FLEXRuntimeUtilityErrorCodeDoesNotRecognizeSelector userInfo:userInfo];
  258. }
  259. return nil;
  260. }
  261. // Build the invocation
  262. NSMethodSignature *methodSignature = [object methodSignatureForSelector:selector];
  263. NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];
  264. [invocation setSelector:selector];
  265. [invocation setTarget:object];
  266. [invocation retainArguments];
  267. // Always self and _cmd
  268. NSUInteger numberOfArguments = [methodSignature numberOfArguments];
  269. for (NSUInteger argumentIndex = kFLEXNumberOfImplicitArgs; argumentIndex < numberOfArguments; argumentIndex++) {
  270. NSUInteger argumentsArrayIndex = argumentIndex - kFLEXNumberOfImplicitArgs;
  271. id argumentObject = [arguments count] > argumentsArrayIndex ? arguments[argumentsArrayIndex] : nil;
  272. // NSNull in the arguments array can be passed as a placeholder to indicate nil. We only need to set the argument if it will be non-nil.
  273. if (argumentObject && ![argumentObject isKindOfClass:[NSNull class]]) {
  274. const char *typeEncodingCString = [methodSignature getArgumentTypeAtIndex:argumentIndex];
  275. if (typeEncodingCString[0] == @encode(id)[0] || typeEncodingCString[0] == @encode(Class)[0] || [self isTollFreeBridgedValue:argumentObject forCFType:typeEncodingCString]) {
  276. // Object
  277. [invocation setArgument:&argumentObject atIndex:argumentIndex];
  278. } else if (strcmp(typeEncodingCString, @encode(CGColorRef)) == 0 && [argumentObject isKindOfClass:[UIColor class]]) {
  279. // Bridging UIColor to CGColorRef
  280. CGColorRef colorRef = [argumentObject CGColor];
  281. [invocation setArgument:&colorRef atIndex:argumentIndex];
  282. } else if ([argumentObject isKindOfClass:[NSValue class]]) {
  283. // Primitive boxed in NSValue
  284. NSValue *argumentValue = (NSValue *)argumentObject;
  285. // Ensure that the type encoding on the NSValue matches the type encoding of the argument in the method signature
  286. if (strcmp([argumentValue objCType], typeEncodingCString) != 0) {
  287. if (error) {
  288. NSDictionary *userInfo = @{ NSLocalizedDescriptionKey : [NSString stringWithFormat:@"Type encoding mismatch for agrument at index %lu. Value type: %s; Method argument type: %s.", (unsigned long)argumentsArrayIndex, [argumentValue objCType], typeEncodingCString]};
  289. *error = [NSError errorWithDomain:FLEXRuntimeUtilityErrorDomain code:FLEXRuntimeUtilityErrorCodeArgumentTypeMismatch userInfo:userInfo];
  290. }
  291. return nil;
  292. }
  293. NSUInteger bufferSize = 0;
  294. @try {
  295. // NSGetSizeAndAlignment barfs on type encoding for bitfields.
  296. NSGetSizeAndAlignment(typeEncodingCString, &bufferSize, NULL);
  297. } @catch (NSException *exception) { }
  298. if (bufferSize > 0) {
  299. void *buffer = calloc(bufferSize, 1);
  300. [argumentValue getValue:buffer];
  301. [invocation setArgument:buffer atIndex:argumentIndex];
  302. free(buffer);
  303. }
  304. }
  305. }
  306. }
  307. // Try to invoke the invocation but guard against an exception being thrown.
  308. BOOL successfullyInvoked = NO;
  309. @try {
  310. // Some methods are not fit to be called...
  311. // Looking at you -[UIResponder(UITextInputAdditions) _caretRect]
  312. [invocation invoke];
  313. successfullyInvoked = YES;
  314. } @catch (NSException *exception) {
  315. // Bummer...
  316. if (error) {
  317. NSDictionary *userInfo = @{ NSLocalizedDescriptionKey : [NSString stringWithFormat:@"Exception thrown while performing selector %@ on object %@", NSStringFromSelector(selector), object]};
  318. *error = [NSError errorWithDomain:FLEXRuntimeUtilityErrorDomain code:FLEXRuntimeUtilityErrorCodeInvocationFailed userInfo:userInfo];
  319. }
  320. }
  321. // Retreive the return value and box if necessary.
  322. id returnObject = nil;
  323. if (successfullyInvoked) {
  324. const char *returnType = [methodSignature methodReturnType];
  325. if (returnType[0] == @encode(id)[0] || returnType[0] == @encode(Class)[0]) {
  326. __unsafe_unretained id objectReturnedFromMethod = nil;
  327. [invocation getReturnValue:&objectReturnedFromMethod];
  328. returnObject = objectReturnedFromMethod;
  329. } else if (returnType[0] != @encode(void)[0]) {
  330. void *returnValue = malloc([methodSignature methodReturnLength]);
  331. if (returnValue) {
  332. [invocation getReturnValue:returnValue];
  333. returnObject = [self valueForPrimitivePointer:returnValue objCType:returnType];
  334. free(returnValue);
  335. }
  336. }
  337. }
  338. return returnObject;
  339. }
  340. + (BOOL)isTollFreeBridgedValue:(id)value forCFType:(const char *)typeEncoding
  341. {
  342. // See https://developer.apple.com/library/ios/documentation/general/conceptual/CocoaEncyclopedia/Toll-FreeBridgin/Toll-FreeBridgin.html
  343. #define CASE(cftype, foundationClass) \
  344. if(strcmp(typeEncoding, @encode(cftype)) == 0) { \
  345. return [value isKindOfClass:[foundationClass class]]; \
  346. }
  347. CASE(CFArrayRef, NSArray);
  348. CASE(CFAttributedStringRef, NSAttributedString);
  349. CASE(CFCalendarRef, NSCalendar);
  350. CASE(CFCharacterSetRef, NSCharacterSet);
  351. CASE(CFDataRef, NSData);
  352. CASE(CFDateRef, NSDate);
  353. CASE(CFDictionaryRef, NSDictionary);
  354. CASE(CFErrorRef, NSError);
  355. CASE(CFLocaleRef, NSLocale);
  356. CASE(CFMutableArrayRef, NSMutableArray);
  357. CASE(CFMutableAttributedStringRef, NSMutableAttributedString);
  358. CASE(CFMutableCharacterSetRef, NSMutableCharacterSet);
  359. CASE(CFMutableDataRef, NSMutableData);
  360. CASE(CFMutableDictionaryRef, NSMutableDictionary);
  361. CASE(CFMutableSetRef, NSMutableSet);
  362. CASE(CFMutableStringRef, NSMutableString);
  363. CASE(CFNumberRef, NSNumber);
  364. CASE(CFReadStreamRef, NSInputStream);
  365. CASE(CFRunLoopTimerRef, NSTimer);
  366. CASE(CFSetRef, NSSet);
  367. CASE(CFStringRef, NSString);
  368. CASE(CFTimeZoneRef, NSTimeZone);
  369. CASE(CFURLRef, NSURL);
  370. CASE(CFWriteStreamRef, NSOutputStream);
  371. #undef CASE
  372. return NO;
  373. }
  374. + (NSString *)editableJSONStringForObject:(id)object
  375. {
  376. NSString *editableDescription = nil;
  377. if (object) {
  378. // This is a hack to use JSON serialization for our editable objects.
  379. // NSJSONSerialization doesn't allow writing fragments - the top level object must be an array or dictionary.
  380. // We always wrap the object inside an array and then strip the outer square braces off the final string.
  381. NSArray *wrappedObject = @[object];
  382. if ([NSJSONSerialization isValidJSONObject:wrappedObject]) {
  383. NSString *wrappedDescription = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:wrappedObject options:0 error:NULL] encoding:NSUTF8StringEncoding];
  384. editableDescription = [wrappedDescription substringWithRange:NSMakeRange(1, [wrappedDescription length] - 2)];
  385. }
  386. }
  387. return editableDescription;
  388. }
  389. + (id)objectValueFromEditableJSONString:(NSString *)string
  390. {
  391. id value = nil;
  392. // nil for empty string/whitespace
  393. if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length] > 0) {
  394. value = [NSJSONSerialization JSONObjectWithData:[string dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:NULL];
  395. }
  396. return value;
  397. }
  398. + (NSValue *)valueForNumberWithObjCType:(const char *)typeEncoding fromInputString:(NSString *)inputString
  399. {
  400. NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
  401. [formatter setNumberStyle:NSNumberFormatterDecimalStyle];
  402. NSNumber *number = [formatter numberFromString:inputString];
  403. // Make sure we box the number with the correct type encoding so it can be propperly unboxed later via getValue:
  404. NSValue *value = nil;
  405. if (strcmp(typeEncoding, @encode(char)) == 0) {
  406. char primitiveValue = [number charValue];
  407. value = [NSValue value:&primitiveValue withObjCType:typeEncoding];
  408. } else if (strcmp(typeEncoding, @encode(int)) == 0) {
  409. int primitiveValue = [number intValue];
  410. value = [NSValue value:&primitiveValue withObjCType:typeEncoding];
  411. } else if (strcmp(typeEncoding, @encode(short)) == 0) {
  412. short primitiveValue = [number shortValue];
  413. value = [NSValue value:&primitiveValue withObjCType:typeEncoding];
  414. } else if (strcmp(typeEncoding, @encode(long)) == 0) {
  415. long primitiveValue = [number longValue];
  416. value = [NSValue value:&primitiveValue withObjCType:typeEncoding];
  417. } else if (strcmp(typeEncoding, @encode(long long)) == 0) {
  418. long long primitiveValue = [number longLongValue];
  419. value = [NSValue value:&primitiveValue withObjCType:typeEncoding];
  420. } else if (strcmp(typeEncoding, @encode(unsigned char)) == 0) {
  421. unsigned char primitiveValue = [number unsignedCharValue];
  422. value = [NSValue value:&primitiveValue withObjCType:typeEncoding];
  423. } else if (strcmp(typeEncoding, @encode(unsigned int)) == 0) {
  424. unsigned int primitiveValue = [number unsignedIntValue];
  425. value = [NSValue value:&primitiveValue withObjCType:typeEncoding];
  426. } else if (strcmp(typeEncoding, @encode(unsigned short)) == 0) {
  427. unsigned short primitiveValue = [number unsignedShortValue];
  428. value = [NSValue value:&primitiveValue withObjCType:typeEncoding];
  429. } else if (strcmp(typeEncoding, @encode(unsigned long)) == 0) {
  430. unsigned long primitiveValue = [number unsignedLongValue];
  431. value = [NSValue value:&primitiveValue withObjCType:typeEncoding];
  432. } else if (strcmp(typeEncoding, @encode(unsigned long long)) == 0) {
  433. unsigned long long primitiveValue = [number unsignedLongValue];
  434. value = [NSValue value:&primitiveValue withObjCType:typeEncoding];
  435. } else if (strcmp(typeEncoding, @encode(float)) == 0) {
  436. float primitiveValue = [number floatValue];
  437. value = [NSValue value:&primitiveValue withObjCType:typeEncoding];
  438. } else if (strcmp(typeEncoding, @encode(double)) == 0) {
  439. double primitiveValue = [number doubleValue];
  440. value = [NSValue value:&primitiveValue withObjCType:typeEncoding];
  441. }
  442. return value;
  443. }
  444. + (void)enumerateTypesInStructEncoding:(const char *)structEncoding usingBlock:(void (^)(NSString *structName, const char *fieldTypeEncoding, NSString *prettyTypeEncoding, NSUInteger fieldIndex, NSUInteger fieldOffset))typeBlock
  445. {
  446. if (structEncoding && structEncoding[0] == '{') {
  447. const char *equals = strchr(structEncoding, '=');
  448. if (equals) {
  449. const char *nameStart = structEncoding + 1;
  450. NSString *structName = [@(structEncoding) substringWithRange:NSMakeRange(nameStart - structEncoding, equals - nameStart)];
  451. NSUInteger fieldAlignment = 0;
  452. NSUInteger structSize = 0;
  453. @try {
  454. // NSGetSizeAndAlignment barfs on type encoding for bitfields.
  455. NSGetSizeAndAlignment(structEncoding, &structSize, &fieldAlignment);
  456. } @catch (NSException *exception) { }
  457. if (structSize > 0) {
  458. NSUInteger runningFieldIndex = 0;
  459. NSUInteger runningFieldOffset = 0;
  460. const char *typeStart = equals + 1;
  461. while (*typeStart != '}') {
  462. NSUInteger fieldSize = 0;
  463. // If the struct type encoding was successfully handled by NSGetSizeAndAlignment above, we *should* be ok with the field here.
  464. const char *nextTypeStart = NSGetSizeAndAlignment(typeStart, &fieldSize, NULL);
  465. NSString *typeEncoding = [@(structEncoding) substringWithRange:NSMakeRange(typeStart - structEncoding, nextTypeStart - typeStart)];
  466. typeBlock(structName, [typeEncoding UTF8String], [self readableTypeForEncoding:typeEncoding], runningFieldIndex, runningFieldOffset);
  467. runningFieldOffset += fieldSize;
  468. // Padding to keep propper alignment. __attribute((packed)) structs will break here.
  469. // The type encoding is no different for packed structs, so it's not clear there's anything we can do for those.
  470. if (runningFieldOffset % fieldAlignment != 0) {
  471. runningFieldOffset += fieldAlignment - runningFieldOffset % fieldAlignment;
  472. }
  473. runningFieldIndex++;
  474. typeStart = nextTypeStart;
  475. }
  476. }
  477. }
  478. }
  479. }
  480. #pragma mark - Internal Helpers
  481. + (NSDictionary *)attributesDictionaryForProperty:(objc_property_t)property
  482. {
  483. NSString *attributes = @(property_getAttributes(property));
  484. // Thanks to MAObjcRuntime for inspiration here.
  485. NSArray *attributePairs = [attributes componentsSeparatedByString:@","];
  486. NSMutableDictionary *attributesDictionary = [NSMutableDictionary dictionaryWithCapacity:[attributePairs count]];
  487. for (NSString *attributePair in attributePairs) {
  488. [attributesDictionary setObject:[attributePair substringFromIndex:1] forKey:[attributePair substringToIndex:1]];
  489. }
  490. return attributesDictionary;
  491. }
  492. + (NSString *)appendName:(NSString *)name toType:(NSString *)type
  493. {
  494. NSString *combined = nil;
  495. if ([type characterAtIndex:[type length] - 1] == '*') {
  496. combined = [type stringByAppendingString:name];
  497. } else {
  498. combined = [type stringByAppendingFormat:@" %@", name];
  499. }
  500. return combined;
  501. }
  502. + (NSString *)readableTypeForEncoding:(NSString *)encodingString
  503. {
  504. if (!encodingString) {
  505. return nil;
  506. }
  507. // See https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html
  508. // class-dump has a much nicer and much more complete implementation for this task, but it is distributed under GPLv2 :/
  509. // See https://github.com/nygard/class-dump/blob/master/Source/CDType.m
  510. // Warning: this method uses multiple middle returns and macros to cut down on boilerplate.
  511. // The use of macros here was inspired by https://www.mikeash.com/pyblog/friday-qa-2013-02-08-lets-build-key-value-coding.html
  512. const char *encodingCString = [encodingString UTF8String];
  513. // Objects
  514. if (encodingCString[0] == '@') {
  515. NSString *class = [encodingString substringFromIndex:1];
  516. class = [class stringByReplacingOccurrencesOfString:@"\"" withString:@""];
  517. if ([class length] == 0 || [class isEqual:@"?"]) {
  518. class = @"id";
  519. } else {
  520. class = [class stringByAppendingString:@" *"];
  521. }
  522. return class;
  523. }
  524. // C Types
  525. #define TRANSLATE(ctype) \
  526. if (strcmp(encodingCString, @encode(ctype)) == 0) { \
  527. return (NSString *)CFSTR(#ctype); \
  528. }
  529. // Order matters here since some of the cocoa types are typedefed to c types.
  530. // We can't recover the exact mapping, but we choose to prefer the cocoa types.
  531. // This is not an exhaustive list, but it covers the most common types
  532. TRANSLATE(CGRect);
  533. TRANSLATE(CGPoint);
  534. TRANSLATE(CGSize);
  535. TRANSLATE(UIEdgeInsets);
  536. TRANSLATE(UIOffset);
  537. TRANSLATE(NSRange);
  538. TRANSLATE(CGAffineTransform);
  539. TRANSLATE(CATransform3D);
  540. TRANSLATE(CGColorRef);
  541. TRANSLATE(CGPathRef);
  542. TRANSLATE(CGContextRef);
  543. TRANSLATE(NSInteger);
  544. TRANSLATE(NSUInteger);
  545. TRANSLATE(CGFloat);
  546. TRANSLATE(BOOL);
  547. TRANSLATE(int);
  548. TRANSLATE(short);
  549. TRANSLATE(long);
  550. TRANSLATE(long long);
  551. TRANSLATE(unsigned char);
  552. TRANSLATE(unsigned int);
  553. TRANSLATE(unsigned short);
  554. TRANSLATE(unsigned long);
  555. TRANSLATE(unsigned long long);
  556. TRANSLATE(float);
  557. TRANSLATE(double);
  558. TRANSLATE(long double);
  559. TRANSLATE(char *);
  560. TRANSLATE(Class);
  561. TRANSLATE(objc_property_t);
  562. TRANSLATE(Ivar);
  563. TRANSLATE(Method);
  564. TRANSLATE(Category);
  565. TRANSLATE(NSZone *);
  566. TRANSLATE(SEL);
  567. TRANSLATE(void);
  568. #undef TRANSLATE
  569. // Qualifier Prefixes
  570. // Do this after the checks above since some of the direct translations (i.e. Method) contain a prefix.
  571. #define RECURSIVE_TRANSLATE(prefix, formatString) \
  572. if (encodingCString[0] == prefix) { \
  573. NSString *recursiveType = [self readableTypeForEncoding:[encodingString substringFromIndex:1]]; \
  574. return [NSString stringWithFormat:formatString, recursiveType]; \
  575. }
  576. // If there's a qualifier prefix on the encoding, translate it and then
  577. // recursively call this method with the rest of the encoding string.
  578. RECURSIVE_TRANSLATE('^', @"%@ *");
  579. RECURSIVE_TRANSLATE('r', @"const %@");
  580. RECURSIVE_TRANSLATE('n', @"in %@");
  581. RECURSIVE_TRANSLATE('N', @"inout %@");
  582. RECURSIVE_TRANSLATE('o', @"out %@");
  583. RECURSIVE_TRANSLATE('O', @"bycopy %@");
  584. RECURSIVE_TRANSLATE('R', @"byref %@");
  585. RECURSIVE_TRANSLATE('V', @"oneway %@");
  586. RECURSIVE_TRANSLATE('b', @"bitfield(%@)");
  587. #undef RECURSIVE_TRANSLATE
  588. // If we couldn't translate, just return the original encoding string
  589. return encodingString;
  590. }
  591. + (NSValue *)valueForPrimitivePointer:(void *)pointer objCType:(const char *)type
  592. {
  593. // CASE macro inspired by https://www.mikeash.com/pyblog/friday-qa-2013-02-08-lets-build-key-value-coding.html
  594. #define CASE(ctype, selectorpart) \
  595. if(strcmp(type, @encode(ctype)) == 0) { \
  596. return [NSNumber numberWith ## selectorpart: *(ctype *)pointer]; \
  597. }
  598. CASE(BOOL, Bool);
  599. CASE(unsigned char, UnsignedChar);
  600. CASE(short, Short);
  601. CASE(unsigned short, UnsignedShort);
  602. CASE(int, Int);
  603. CASE(unsigned int, UnsignedInt);
  604. CASE(long, Long);
  605. CASE(unsigned long, UnsignedLong);
  606. CASE(long long, LongLong);
  607. CASE(unsigned long long, UnsignedLongLong);
  608. CASE(float, Float);
  609. CASE(double, Double);
  610. #undef CASE
  611. NSValue *value = nil;
  612. @try {
  613. value = [NSValue valueWithBytes:pointer objCType:type];
  614. } @catch (NSException *exception) {
  615. // Certain type encodings are not supported by valueWithBytes:objCType:. Just fail silently if an exception is thrown.
  616. }
  617. return value;
  618. }
  619. @end