iOS 解决数组闪退的问题


#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

@interface NSArray (SafeAccess)

@end


@interface NSMutableArray (SafeAccess)

@end


NS_ASSUME_NONNULL_END



#import "NSArray+SafeAccess.h"
#import <objc/runtime.h>

@implementation NSMutableArray (SafeAccess)

+(void)load{

    /// __NSArrayM  objectAtIndex
    Class class = NSClassFromString(@"__NSArrayM");

    SEL originalSelector = @selector(objectAtIndex:);
    SEL swizzledSelector = @selector(swizzled_M_objectAtIndex:);
    
    Method originalMethod = class_getInstanceMethod(class, originalSelector);
    Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
    
    BOOL success = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
    if (success) {
        class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
    } else {
        method_exchangeImplementations(originalMethod, swizzledMethod);
    }
    
    /// __NSArrayM  insertObject
    SEL originalSelector2 = @selector(insertObject:atIndex:);
    SEL swizzledSelector2 = @selector(swizzled_M_insertObject:atIndex:);
    
    Method originalMethod2 = class_getInstanceMethod(class, originalSelector2);
    Method swizzledMethod2 = class_getInstanceMethod(class, swizzledSelector2);
    
     success = class_addMethod(class, originalSelector2, method_getImplementation(swizzledMethod2), method_getTypeEncoding(swizzledMethod2));
    if (success) {
        class_replaceMethod(class, swizzledSelector2, method_getImplementation(originalMethod2), method_getTypeEncoding(originalMethod2));
    } else {
        method_exchangeImplementations(originalMethod2, swizzledMethod2);
    }
}


-(id)swizzled_M_objectAtIndex:(NSInteger)index{
    if (index >= self.count) {
        NSLog(@"数组越界 %@---index:%zi",self,index);
        return nil;
     }
     return [self swizzled_M_objectAtIndex:index];
}

- (void)swizzled_M_insertObject:(id)anObject atIndex:(NSUInteger)index{
    /// 只有有值才把数据添加到数组中
    if(anObject){
        [self swizzled_M_insertObject:anObject atIndex:index];
    }else{
        NSLog(@"插入的数据为%@",anObject);
    }
}

@end

@implementation NSArray (SafeAccess)

+(void)load{
    
    // __NSArrayI  objectAtIndex
    Class class = NSClassFromString(@"__NSArrayI");

    SEL originalSelector = @selector(objectAtIndex:);
    SEL swizzledSelector = @selector(swizzled_I_objectAtIndex:);
    
    Method originalMethod = class_getInstanceMethod(class, originalSelector);
    Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
    
    BOOL success = class_addMethod(class, originalSelector, method_getImplementation(swizzledMethod), method_getTypeEncoding(swizzledMethod));
    if (success) {
        class_replaceMethod(class, swizzledSelector, method_getImplementation(originalMethod), method_getTypeEncoding(originalMethod));
    } else {
        method_exchangeImplementations(originalMethod, swizzledMethod);
    }
}

-(id)swizzled_I_objectAtIndex:(NSInteger)index{
    if (index >= self.count) {
        NSLog(@"数组越界 %@---index:%zi",self,index);
        return nil;
     }
     return [self swizzled_I_objectAtIndex:index];
}

@end

posted @ 2023-03-30 13:54  qqcc1388  阅读(56)  评论(0)    收藏  举报