123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- //
- // NSMutableArray+RQExtension.m
- // XinShouJiaDao
- //
- // Created by 张嵘 on 2021/7/7.
- // Copyright © 2021 JCZ. All rights reserved.
- //
- #import "NSMutableArray+RQExtension.h"
- @implementation NSMutableArray (RQExtension)
- static char const *name = "__NSArrayM";
- #pragma mark - Safe
- + (void)load {
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- [objc_getClass(name) methodSwizzlingWithOriginalSelector:@selector(addObject:) bySwizzledSelector:@selector(safeAddObject:)];
- [objc_getClass(name) methodSwizzlingWithOriginalSelector:@selector(removeObject:) bySwizzledSelector:@selector(safeRemoveObject:)];
- [objc_getClass(name) methodSwizzlingWithOriginalSelector:@selector(removeObjectAtIndex:) bySwizzledSelector:@selector(safeRemoveObjectAtIndex:)];
- [objc_getClass(name) methodSwizzlingWithOriginalSelector:@selector(insertObject:atIndex:) bySwizzledSelector:@selector(safeInsertObject:atIndex:)];
- [objc_getClass(name) methodSwizzlingWithOriginalSelector:@selector(objectAtIndex:) bySwizzledSelector:@selector(safeObjectAtIndex:)];
- });
- }
- - (void)safeAddObject:(id)obj {
- if (obj == nil) {
- NSLog(@"%s can add nil object into NSMutableArray", __FUNCTION__);
- return;
- }
- [self safeAddObject:obj];
- }
- - (void)safeRemoveObject:(id)obj {
- if (obj == nil) {
- NSLog(@"%s call -removeObject:, but argument obj is nil", __FUNCTION__);
- return;
- }
- [self safeRemoveObject:obj];
- }
- - (void)safeRemoveObjectAtIndex:(NSUInteger)index {
- if (self.count <= 0) {
- NSLog(@"%s can't get any object from an empty array", __FUNCTION__);
- } else if (index >= self.count) {
- NSLog(@"%s index out of bound", __FUNCTION__);
- } else {
- [self safeRemoveObjectAtIndex:index];
- }
- }
- - (void)safeInsertObject:(id)anObject atIndex:(NSUInteger)index {
- if (anObject == nil) {
- NSLog(@"%s can't insert nil into NSMutableArray", __FUNCTION__);
- } else if (index > self.count) {
- NSLog(@"%s: index is invalid", __FUNCTION__);
- } else {
- [self safeInsertObject:anObject atIndex:index];
- }
- }
- - (id)safeObjectAtIndex:(NSUInteger)index {
- if (self.count == 0) {
- NSLog(@"%s can't get any object from an empty array", __FUNCTION__);
- return nil;
- } else if (index > self.count - 1) {
- // [__NSArrayM objectAtIndex:]: index 2 beyond bounds [0 .. 1]
- NSLog(@"%s: index %ld beyond bounds [0..%ld]", __FUNCTION__, index, self.count);
- return nil;
- } else {
- return [self safeObjectAtIndex:index];
- }
- }
- #pragma mark - Other
- -(void)property:(id)obj forKey:(NSString*)key
- {
- if (self)
- {
- if (!obj) {
- obj = @"";
- }
- if (!key) {
- key = @"";
- }
- NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:obj,key, nil];
- [self addObject:dict];
- }
- }
- -(void)addPro:(NSString*)name Value:(NSString*)value
- {
- [self property:value forKey:name];
- }
- -(void)addPro2:(NSString*)name Value:(id)value
- {
- [self property:value forKey:name];
- }
- @end
|