NSString+ObjcRuntime.m 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //
  2. // NSString+ObjcRuntime.m
  3. // FLEX
  4. //
  5. // Derived from MirrorKit.
  6. // Created by Tanner on 7/1/15.
  7. // Copyright (c) 2020 FLEX Team. 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 new];
  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. // Note: the type encoding here is not always correct. Radar: FB7499230
  28. attributes[kFLEXPropertyAttributeKeyTypeEncoding] = [attribute stringbyDeletingCharacterAtIndex:0];
  29. break;
  30. case FLEXPropertyAttributeBackingIvarName:
  31. attributes[kFLEXPropertyAttributeKeyBackingIvarName] = [attribute stringbyDeletingCharacterAtIndex:0];
  32. break;
  33. case FLEXPropertyAttributeCopy:
  34. attributes[kFLEXPropertyAttributeKeyCopy] = @YES;
  35. break;
  36. case FLEXPropertyAttributeCustomGetter:
  37. attributes[kFLEXPropertyAttributeKeyCustomGetter] = [attribute stringbyDeletingCharacterAtIndex:0];
  38. break;
  39. case FLEXPropertyAttributeCustomSetter:
  40. attributes[kFLEXPropertyAttributeKeyCustomSetter] = [attribute stringbyDeletingCharacterAtIndex:0];
  41. break;
  42. case FLEXPropertyAttributeDynamic:
  43. attributes[kFLEXPropertyAttributeKeyDynamic] = @YES;
  44. break;
  45. case FLEXPropertyAttributeGarbageCollectible:
  46. attributes[kFLEXPropertyAttributeKeyGarbageCollectable] = @YES;
  47. break;
  48. case FLEXPropertyAttributeNonAtomic:
  49. attributes[kFLEXPropertyAttributeKeyNonAtomic] = @YES;
  50. break;
  51. case FLEXPropertyAttributeOldTypeEncoding:
  52. attributes[kFLEXPropertyAttributeKeyOldStyleTypeEncoding] = [attribute stringbyDeletingCharacterAtIndex:0];
  53. break;
  54. case FLEXPropertyAttributeReadOnly:
  55. attributes[kFLEXPropertyAttributeKeyReadOnly] = @YES;
  56. break;
  57. case FLEXPropertyAttributeRetain:
  58. attributes[kFLEXPropertyAttributeKeyRetain] = @YES;
  59. break;
  60. case FLEXPropertyAttributeWeak:
  61. attributes[kFLEXPropertyAttributeKeyWeak] = @YES;
  62. break;
  63. }
  64. }
  65. return attributes;
  66. }
  67. @end