NSMutableArray+RQExtension.m 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //
  2. // NSMutableArray+RQExtension.m
  3. // XinShouJiaDao
  4. //
  5. // Created by 张嵘 on 2021/7/7.
  6. // Copyright © 2021 JCZ. All rights reserved.
  7. //
  8. #import "NSMutableArray+RQExtension.h"
  9. @implementation NSMutableArray (RQExtension)
  10. static char const *name = "__NSArrayM";
  11. #pragma mark - Safe
  12. + (void)load {
  13. static dispatch_once_t onceToken;
  14. dispatch_once(&onceToken, ^{
  15. [objc_getClass(name) methodSwizzlingWithOriginalSelector:@selector(addObject:) bySwizzledSelector:@selector(safeAddObject:)];
  16. [objc_getClass(name) methodSwizzlingWithOriginalSelector:@selector(removeObject:) bySwizzledSelector:@selector(safeRemoveObject:)];
  17. [objc_getClass(name) methodSwizzlingWithOriginalSelector:@selector(removeObjectAtIndex:) bySwizzledSelector:@selector(safeRemoveObjectAtIndex:)];
  18. [objc_getClass(name) methodSwizzlingWithOriginalSelector:@selector(insertObject:atIndex:) bySwizzledSelector:@selector(safeInsertObject:atIndex:)];
  19. [objc_getClass(name) methodSwizzlingWithOriginalSelector:@selector(objectAtIndex:) bySwizzledSelector:@selector(safeObjectAtIndex:)];
  20. });
  21. }
  22. - (void)safeAddObject:(id)obj {
  23. if (obj == nil) {
  24. NSLog(@"%s can add nil object into NSMutableArray", __FUNCTION__);
  25. return;
  26. }
  27. [self safeAddObject:obj];
  28. }
  29. - (void)safeRemoveObject:(id)obj {
  30. if (obj == nil) {
  31. NSLog(@"%s call -removeObject:, but argument obj is nil", __FUNCTION__);
  32. return;
  33. }
  34. [self safeRemoveObject:obj];
  35. }
  36. - (void)safeRemoveObjectAtIndex:(NSUInteger)index {
  37. if (self.count <= 0) {
  38. NSLog(@"%s can't get any object from an empty array", __FUNCTION__);
  39. } else if (index >= self.count) {
  40. NSLog(@"%s index out of bound", __FUNCTION__);
  41. } else {
  42. [self safeRemoveObjectAtIndex:index];
  43. }
  44. }
  45. - (void)safeInsertObject:(id)anObject atIndex:(NSUInteger)index {
  46. if (anObject == nil) {
  47. NSLog(@"%s can't insert nil into NSMutableArray", __FUNCTION__);
  48. } else if (index > self.count) {
  49. NSLog(@"%s: index is invalid", __FUNCTION__);
  50. } else {
  51. [self safeInsertObject:anObject atIndex:index];
  52. }
  53. }
  54. - (id)safeObjectAtIndex:(NSUInteger)index {
  55. if (self.count == 0) {
  56. NSLog(@"%s can't get any object from an empty array", __FUNCTION__);
  57. return nil;
  58. } else if (index > self.count - 1) {
  59. // [__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]
  60. NSLog(@"%s: index %ld beyond bounds [0..%ld]", __FUNCTION__, index, self.count);
  61. return nil;
  62. } else {
  63. return [self safeObjectAtIndex:index];
  64. }
  65. }
  66. #pragma mark - Other
  67. -(void)property:(id)obj forKey:(NSString*)key
  68. {
  69. if (self)
  70. {
  71. if (!obj) {
  72. obj = @"";
  73. }
  74. if (!key) {
  75. key = @"";
  76. }
  77. NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:obj,key, nil];
  78. [self addObject:dict];
  79. }
  80. }
  81. -(void)addPro:(NSString*)name Value:(NSString*)value
  82. {
  83. [self property:value forKey:name];
  84. }
  85. -(void)addPro2:(NSString*)name Value:(id)value
  86. {
  87. [self property:value forKey:name];
  88. }
  89. @end