NSArray+TBXSafe.m 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //
  2. // NSArray+TBXSafe.m
  3. // assemble
  4. //
  5. // Created by apple on 2020/11/2.
  6. // Copyright © 2020 kingsunsoft. All rights reserved.
  7. //
  8. #import "NSArray+TBXSafe.h"
  9. #import <objc/runtime.h>
  10. #import "NSObject+Swizzling.h"
  11. @implementation NSArray (TBXSafe)
  12. + (void)load {
  13. //只执行一次这个方法
  14. static dispatch_once_t onceToken;
  15. dispatch_once(&onceToken, ^{
  16. //替换 objectAtIndex
  17. NSString *tmpStr = @"objectAtIndex:";
  18. NSString *tmpFirstStr = @"safe_ZeroObjectAtIndex:";
  19. NSString *tmpThreeStr = @"safe_objectAtIndex:";
  20. NSString *tmpSecondStr = @"safe_singleObjectAtIndex:";
  21. // 替换 objectAtIndexedSubscript
  22. NSString *tmpSubscriptStr = @"objectAtIndexedSubscript:";
  23. NSString *tmpSecondSubscriptStr = @"safe_objectAtIndexedSubscript:";
  24. [NSObject exchangeInstanceMethodWithSelfClass:NSClassFromString(@"__NSArray0")
  25. originalSelector:NSSelectorFromString(tmpStr) swizzledSelector:NSSelectorFromString(tmpFirstStr)];
  26. [NSObject exchangeInstanceMethodWithSelfClass:NSClassFromString(@"__NSSingleObjectArrayI")
  27. originalSelector:NSSelectorFromString(tmpStr) swizzledSelector:NSSelectorFromString(tmpSecondStr)];
  28. [NSObject exchangeInstanceMethodWithSelfClass:NSClassFromString(@"__NSArrayI")
  29. originalSelector:NSSelectorFromString(tmpStr) swizzledSelector:NSSelectorFromString(tmpThreeStr)];
  30. [NSObject exchangeInstanceMethodWithSelfClass:NSClassFromString(@"__NSArrayI")
  31. originalSelector:NSSelectorFromString(tmpSubscriptStr) swizzledSelector:NSSelectorFromString(tmpSecondSubscriptStr)];
  32. });
  33. }
  34. #pragma mark --- implement method
  35. /**
  36. 取出NSArray 第index个 值 对应 __NSArrayI
  37. @param index 索引 index
  38. @return 返回值
  39. */
  40. - (id)safe_objectAtIndex:(NSUInteger)index {
  41. if (index >= self.count){
  42. return nil;
  43. }
  44. return [self safe_objectAtIndex:index];
  45. }
  46. /**
  47. 取出NSArray 第index个 值 对应 __NSSingleObjectArrayI
  48. @param index 索引 index
  49. @return 返回值
  50. */
  51. - (id)safe_singleObjectAtIndex:(NSUInteger)index {
  52. if (index >= self.count){
  53. return nil;
  54. }
  55. return [self safe_singleObjectAtIndex:index];
  56. }
  57. /**
  58. 取出NSArray 第index个 值 对应 __NSArray0
  59. @param index 索引 index
  60. @return 返回值
  61. */
  62. - (id)safe_ZeroObjectAtIndex:(NSUInteger)index {
  63. if (index >= self.count){
  64. return nil;
  65. }
  66. return [self safe_ZeroObjectAtIndex:index];
  67. }
  68. /**
  69. 取出NSArray 第index个 值 对应 __NSArrayI
  70. @param idx 索引 idx
  71. @return 返回值
  72. */
  73. - (id)safe_objectAtIndexedSubscript:(NSUInteger)idx {
  74. if (idx >= self.count){
  75. return nil;
  76. }
  77. return [self safe_objectAtIndexedSubscript:idx];
  78. }
  79. @end