NSString+ObjcRuntime.m 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //
  2. // NSString+ObjcRuntime.m
  3. // FLEX
  4. //
  5. // Derived from MirrorKit.
  6. // Created by Tanner on 7/1/15.
  7. // Copyright (c) 2015 Tanner Bennett. All rights reserved.
  8. //
  9. #import "NSString+ObjcRuntime.h"
  10. #import "FLEXRuntimeUtility.h"
  11. @implementation NSString (Utilities)
  12. - (NSString *)stringbyDeletingCharacterAtIndex:(NSUInteger)idx {
  13. NSMutableString *string = self.mutableCopy;
  14. [string replaceCharactersInRange:NSMakeRange(idx, 1) withString:@""];
  15. return string;
  16. }
  17. /// See this link on how to construct a proper attributes string:
  18. /// https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtPropertyIntrospection.html
  19. - (NSDictionary *)propertyAttributes {
  20. if (!self.length) return nil;
  21. NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
  22. NSArray *components = [self componentsSeparatedByString:@","];
  23. for (NSString *attribute in components) {
  24. FLEXPropertyAttribute c = (FLEXPropertyAttribute)[attribute characterAtIndex:0];
  25. switch (c) {
  26. case FLEXPropertyAttributeTypeEncoding:
  27. attributes[kFLEXPropertyAttributeKeyTypeEncoding] = [attribute stringbyDeletingCharacterAtIndex:0];
  28. break;
  29. case FLEXPropertyAttributeBackingIvarName:
  30. attributes[kFLEXPropertyAttributeKeyBackingIvarName] = [attribute stringbyDeletingCharacterAtIndex:0];
  31. break;
  32. case FLEXPropertyAttributeCopy:
  33. attributes[kFLEXPropertyAttributeKeyCopy] = @YES;
  34. break;
  35. case FLEXPropertyAttributeCustomGetter:
  36. attributes[kFLEXPropertyAttributeKeyCustomGetter] = [attribute stringbyDeletingCharacterAtIndex:0];
  37. break;
  38. case FLEXPropertyAttributeCustomSetter:
  39. attributes[kFLEXPropertyAttributeKeyCustomSetter] = [attribute stringbyDeletingCharacterAtIndex:0];
  40. break;
  41. case FLEXPropertyAttributeDynamic:
  42. attributes[kFLEXPropertyAttributeKeyDynamic] = @YES;
  43. break;
  44. case FLEXPropertyAttributeGarbageCollectible:
  45. attributes[kFLEXPropertyAttributeKeyGarbageCollectable] = @YES;
  46. break;
  47. case FLEXPropertyAttributeNonAtomic:
  48. attributes[kFLEXPropertyAttributeKeyNonAtomic] = @YES;
  49. break;
  50. case FLEXPropertyAttributeOldTypeEncoding:
  51. attributes[kFLEXPropertyAttributeKeyOldStyleTypeEncoding] = [attribute stringbyDeletingCharacterAtIndex:0];
  52. break;
  53. case FLEXPropertyAttributeReadOnly:
  54. attributes[kFLEXPropertyAttributeKeyReadOnly] = @YES;
  55. break;
  56. case FLEXPropertyAttributeRetain:
  57. attributes[kFLEXPropertyAttributeKeyRetain] = @YES;
  58. break;
  59. case FLEXPropertyAttributeWeak:
  60. attributes[kFLEXPropertyAttributeKeyWeak] = @YES;
  61. break;
  62. }
  63. }
  64. return attributes;
  65. }
  66. @end