Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

MrLujh/SafeObject

Repository files navigation

SafeObject

支持pod导入

  • pod 'SafeObject'

  • 执行pod search SafeObject提示搜索不到,可以执行以下命令更新本地search_index.json文件

rm ~/Library/Caches/CocoaPods/search_index.json
  • 如果pod search还是搜索不到,执行pod setup命令更新本地spec缓存(可能需要几分钟),然后再搜索就可以了

App常见崩溃,容器越界,字典空值

  • 数组下标越界

示例代码:

- (void)testArrayOutOfBounds
{
 NSArray *testArray = @[@1,@2,@3];
 
 NSNumber *num = testArray[3];
}
  • 字典构造造与修改

示例代码:

- (void)testDicSetNilValueCrash
{
 // 构造不可变字典时 key和value都不能为空
 NSString *nilValue = nil;
 NSString *nilKey = nil;
 NSDictionary *dic1 = @{@"key" : nilValue};
 NSDictionary *dic2 = @{nilKey : @"value"};
}

方法交换

  • Runtime解决数据越界及字典key或value为nil的情况,主要通过Runtime的方法交换实现,可以扩展一下NSObject分类:
+ (void)exchangeInstanceMethodWithSelfClass:(Class)selfClass
 originalSelector:(SEL)originalSelector
 swizzledSelector:(SEL)swizzledSelector {
 
 Method originalMethod = class_getInstanceMethod(selfClass, originalSelector);
 Method swizzledMethod = class_getInstanceMethod(selfClass, swizzledSelector);
 BOOL didAddMethod = class_addMethod(selfClass,
 originalSelector,
 method_getImplementation(swizzledMethod),
 method_getTypeEncoding(swizzledMethod));
 if (didAddMethod) {
 class_replaceMethod(selfClass,
 swizzledSelector,
 method_getImplementation(originalMethod),
 method_getTypeEncoding(originalMethod));
 } else {
 method_exchangeImplementations(originalMethod, swizzledMethod);
 }
}
/**
 在range范围内, 移除掉anObject

 @param anObject 移除的anObject
 @param range 范围
 */
- (void)safeMutable_removeObject:(id)anObject inRange:(NSRange)range {
 if (range.location > self.count) {
 return;
 }
 
 if (range.length > self.count) {
 return;
 }
 
 if ((range.location + range.length) > self.count) {
 return;
 }
 
 if (!anObject){
 return;
 }
 
 
 return [self safeMutable_removeObject:anObject inRange:range];
 
}

About

IOS崩溃异常的处理,防止数组越界,字典空值处理

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

AltStyle によって変換されたページ (->オリジナル) /