NSMutableArray+TBXSafe.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //
  2. // NSMutableArray+TBXSafe.m
  3. // assemble
  4. //
  5. // Created by apple on 2020/11/2.
  6. // Copyright © 2020 kingsunsoft. All rights reserved.
  7. //
  8. #import "NSMutableArray+TBXSafe.h"
  9. #import <objc/runtime.h>
  10. #import "NSObject+Swizzling.h"
  11. @implementation NSMutableArray (TBXSafe)
  12. + (void)load {
  13. //只执行一次这个方法
  14. static dispatch_once_t onceToken;
  15. dispatch_once(&onceToken, ^{
  16. //替换 objectAtIndex:
  17. NSString *tmpGetStr = @"objectAtIndex:";
  18. NSString *tmpSafeGetStr = @"safeMutable_objectAtIndex:";
  19. [NSObject exchangeInstanceMethodWithSelfClass:NSClassFromString(@"__NSArrayM")
  20. originalSelector:NSSelectorFromString(tmpGetStr) swizzledSelector:NSSelectorFromString(tmpSafeGetStr)];
  21. //替换 removeObjectsInRange:
  22. NSString *tmpRemoveStr = @"removeObjectsInRange:";
  23. NSString *tmpSafeRemoveStr = @"safeMutable_removeObjectsInRange:";
  24. [NSObject exchangeInstanceMethodWithSelfClass:NSClassFromString(@"__NSArrayM")
  25. originalSelector:NSSelectorFromString(tmpRemoveStr) swizzledSelector:NSSelectorFromString(tmpSafeRemoveStr)];
  26. //替换 insertObject:atIndex:
  27. NSString *tmpInsertStr = @"insertObject:atIndex:";
  28. NSString *tmpSafeInsertStr = @"safeMutable_insertObject:atIndex:";
  29. [NSObject exchangeInstanceMethodWithSelfClass:NSClassFromString(@"__NSArrayM")
  30. originalSelector:NSSelectorFromString(tmpInsertStr) swizzledSelector:NSSelectorFromString(tmpSafeInsertStr)];
  31. //替换 removeObject:inRange:
  32. NSString *tmpRemoveRangeStr = @"removeObject:inRange:";
  33. NSString *tmpSafeRemoveRangeStr = @"safeMutable_removeObject:inRange:";
  34. [NSObject exchangeInstanceMethodWithSelfClass:NSClassFromString(@"__NSArrayM")
  35. originalSelector:NSSelectorFromString(tmpRemoveRangeStr) swizzledSelector:NSSelectorFromString(tmpSafeRemoveRangeStr)];
  36. // 替换 objectAtIndexedSubscript
  37. NSString *tmpSubscriptStr = @"objectAtIndexedSubscript:";
  38. NSString *tmpSecondSubscriptStr = @"safeMutable_objectAtIndexedSubscript:";
  39. [NSObject exchangeInstanceMethodWithSelfClass:NSClassFromString(@"__NSArrayM")
  40. originalSelector:NSSelectorFromString(tmpSubscriptStr) swizzledSelector:NSSelectorFromString(tmpSecondSubscriptStr)];
  41. });
  42. }
  43. #pragma mark --- implement method
  44. /**
  45. 取出NSArray 第index个 值
  46. @param index 索引 index
  47. @return 返回值
  48. */
  49. - (id)safeMutable_objectAtIndex:(NSUInteger)index {
  50. if (index >= self.count){
  51. return nil;
  52. }
  53. return [self safeMutable_objectAtIndex:index];
  54. }
  55. /**
  56. NSMutableArray 移除 索引 index 对应的 值
  57. @param range 移除 范围
  58. */
  59. - (void)safeMutable_removeObjectsInRange:(NSRange)range {
  60. if (range.location > self.count) {
  61. return;
  62. }
  63. if (range.length > self.count) {
  64. return;
  65. }
  66. if ((range.location + range.length) > self.count) {
  67. return;
  68. }
  69. return [self safeMutable_removeObjectsInRange:range];
  70. }
  71. /**
  72. 在range范围内, 移除掉anObject
  73. @param anObject 移除的anObject
  74. @param range 范围
  75. */
  76. - (void)safeMutable_removeObject:(id)anObject inRange:(NSRange)range {
  77. if (range.location > self.count) {
  78. return;
  79. }
  80. if (range.length > self.count) {
  81. return;
  82. }
  83. if ((range.location + range.length) > self.count) {
  84. return;
  85. }
  86. if (!anObject){
  87. return;
  88. }
  89. return [self safeMutable_removeObject:anObject inRange:range];
  90. }
  91. /**
  92. NSMutableArray 插入 新值 到 索引index 指定位置
  93. @param anObject 新值
  94. @param index 索引 index
  95. */
  96. - (void)safeMutable_insertObject:(id)anObject atIndex:(NSUInteger)index {
  97. if (index > self.count) {
  98. return;
  99. }
  100. if (!anObject){
  101. return;
  102. }
  103. [self safeMutable_insertObject:anObject atIndex:index];
  104. }
  105. /**
  106. 取出NSArray 第index个 值 对应 __NSArrayI
  107. @param idx 索引 idx
  108. @return 返回值
  109. */
  110. - (id)safeMutable_objectAtIndexedSubscript:(NSUInteger)idx {
  111. if (idx >= self.count){
  112. return nil;
  113. }
  114. return [self safeMutable_objectAtIndexedSubscript:idx];
  115. }
  116. @end