安装使用
pod 'YZExtension', '~> 2.0.0'
github demo
说明
在使用 数组或者字典、字符串时,经常遇到越界或者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 [list replaceObjectAtIndex:0 withObject:nil]; // 0x600000448220 [__NSArrayM replaceObjectAtIndex:withObject:] object cannot be nil NSArray *array = @[@"aa",text,@"ccc"]; // 0x6000000031f0 [__NSPlaceholderArray initWithObjects:count:] attempt to insert nil object from objects[1] NSLog(@"%@",array); /* ( aa, ccc ) */
-
字典添加nil对象
NSString *text = nil; [list addObject:@{@"name":text}]; NSDictionary *dic = @{@"name":@"aaa",@"age":text,@"sex":@"man"}; // 0x600000003230 [__NSPlaceholderDictionary initWithObjects:forKeys:count:] attempt to insert nil object from objects[1] NSLog(@"%@",dic); /* { name = aaa; sex = man; } */
-
字典设置 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}];
网友评论