YZExtension 2.0.1

YZExtension 2.0.1

yellowzhou 维护。



  • yellowzhou

YZStrongExtension

安装使用

pod 'YZExtension', '~> 2.0.0'

说明

在使用数组或字典、字符串时,经常遇到越界或nil对象引起的崩溃。在复杂的项目中,无法逐一排查。通过使用 method swizzling 技术实现这类异常的修复,在运行中不会出现崩溃。

  • 数组越界取值
    NSMutableArray *list = [NSMutableArray new];
    id object = [list objectAtIndex:1000];
    NSLog(@"%@",object);// 0x600000446e70 [__NSArrayM objectAtIndex:] index 1000 beyond bounds [0 .. 0]
    
    
  • 数组插入nil对象
    NSMutableArray *list = [NSMutableArray new];
    [list insertObject:nil atIndex:0];
    // 0x600000446e70 [__NSArrayM insertObject:atIndex:] object cannot be nil
    
    
  • 字典添加nil对象
    NSString *text = nil;
    [list addObject:@{@"name":text}];
    
    
  • 字典设置 key、object 为nil对象
    NSMutableDictionary *dict = [NSMutableDictionary new];
    [dict setObject:nil forKey:@"test"];
    [dict setObject:@"object" forKey:text];
    // 0x60400023a060 [__NSDictionaryM setObject:forKey:] key cannot be nil (object: object)
    // 0x60400023a060 [__NSDictionaryM setObject:forKey:] object cannot be nil (key: test)
    
    
  • NSString 截取越界
    NSString *tmp = @"hello wrold";
    NSString *p = [tmp substringFromIndex:100];
    p = [tmp substringToIndex:300];
    p = [tmp substringWithRange:(NSRange){20,40}];