我們強哥說,一款好的app,對于崩潰情況絕對是零容忍的,可以數(shù)據(jù)顯示不出來,讓用戶重新刷新,但不能直接崩潰。因此我們應該盡量減少崩潰。據(jù)統(tǒng)計和自己開發(fā)經(jīng)驗積累,有很多崩潰都是數(shù)組越界或者傳入值為nil導致。因此這邊從NSString、NSMutableString、NSArray、NSMutableArray、NSMutableDictionary,包裝了它們的類方法,從而達到防止崩潰。

圖1.jpeg
一.NSString
1.在NSString類別里添加這四個最常用的操作方法
@interface NSString (Extention)
//判斷是否包含字符串a(chǎn)String
- (BOOL)containSafeString:(NSString *)aString;
//NSString 從截取到某個地方
- (NSString *)substringToSafeIndex:(NSUInteger)to;
//NSString 截取range范圍
- (NSString *)substringWithSafeRange:(NSRange)range;
//NSString 從某個地方開始截取
- (NSString *)substringFromSafeIndex:(NSUInteger)from;
@end
2.實現(xiàn):
/**
判斷是否包含 字符串 aString
@param aString 子字符串
@return 是否包含
*/
- (BOOL)containSafeString:(NSString *)aString {
BOOL isContain = NO;
if([aString isKindOfClass:[NSString class]]) {
isContain = [self containsString:aString];
}
return isContain;
}
/**
從from位置截取字符串
@param from 截取起始位置
@return 截取的子字符串
*/
- (NSString *)substringFromSafeIndex:(NSUInteger)from {
if ([self isKindOfClass:[NSString class]]) {
if (from <= self.length) {
return [self substringFromIndex:from];
}
}
return nil;
}
/**
從開始截取到to位置的字符串
@param to 截取終點位置
@return 返回截取的字符串
*/
- (NSString *)substringToSafeIndex:(NSUInteger)to{
if ([self isKindOfClass:[NSString class]]) {
if (to <= self.length) {
return [self substringToIndex:to];
}
}
return nil;
}
/**
截取指定范圍的字符串
@param range 指定的范圍
@return 返回截取的字符串
*/
- (NSString *)substringWithSafeRange:(NSRange)range{
if ([self isKindOfClass:[NSString class]]) {
if ((range.location + range.length) <= self.length && range.location <= self.length && range.length <= self.length) {
return [self substringWithRange:range];
}
}
return nil;
}
二.NSMutableString
1.由于NSMutableString繼承自NSString,所以NSString擴展類別里面定義的方法同樣適用,現(xiàn)在只擴展了添加方法
@interface NSMutableString (Extention)
// 添加str到字符串末尾
- (void)appendSafeString:(NSString *)str;
@end
2.實現(xiàn)
/**
將str添加到字符串末尾
@param str <#str description#>
*/
- (void)appendSafeString:(NSString *)str {
if ([str isKindOfClass:[NSString class]] && str.length > 0) {
[self appendString:str];
}
}

圖2.jpeg
三.NSArray
1.擴展NSArray獲取值方法
@interface NSArray (NSArrayEx)
// 取出 NSArray 第 index 位置 的 值
- (id)objectAtSafeIndex:(NSUInteger)index;
@end
2.實現(xiàn):
@implementation NSArray (NSArrayEx)
/**
取出NSArray 第index個 值
@param index 索引 index
@return 返回值
*/
- (id) objectAtSafeIndex:(NSUInteger)index {
if([self isKindOfClass:[NSArray class]]) {
if (self.count > index) {
return [self objectAtIndex: index];
}
}
return nil;
}
四.NSMutableArray
1.擴展最常用的添加、移除、插入方法:
@interface NSMutableArray (NSMutableArrayEx)
// 添加 值 到 NSMutableArray 尾部
- (void)addSafeObject:(id)anObject;
// NSMutableArray 移除 第 index 值
- (void)removeSafeObjectAtIndex:(NSUInteger)index;
// 插入 新值 到 NSMutableArray 的 第index 位置
- (void)insertSafeObject:(id)anObject atIndex:(NSUInteger)index;
@end
2.實現(xiàn):
@implementation NSMutableArray (NSMutableArrayEx)
/**
NSMutableArray 添加 新內(nèi)容
@param anObject 新內(nèi)容
*/
- (void)addSafeObject:(id)anObject {
if (anObject) {
[self addObject:anObject];
}
}
/**
NSMutableArray 移除 索引 index 對應的 值
@param index 索引 index
*/
- (void)removeSafeObjectAtIndex:(NSUInteger)index {
if (self.count > index) {
[self removeObjectAtIndex:index];
}
}
/**
NSMutableArray 插入 新值 到 索引index 指定位置
@param anObject 新值
@param index 索引 index
*/
- (void)insertSafeObject:(id)anObject atIndex:(NSUInteger)index {
if (anObject && index <= self.count ) {
[self insertObject:anObject atIndex:index];
}
}
@end
五.NSMutableDictionary
1.擴展NSMutableDictionary設置鍵值對和移除操作:
@interface NSMutableDictionary (NSMutableDictionaryEx)
// NSMutableDictionary 根據(jù) 鍵 移除鍵值對
- (void)removeSafeObjectForKey:(id)aKey;
// NSMutableDictionary 添加新的鍵值對
- (void)setSafeObject:(id)anObject forSafeKey:(id)aKey;
@end
2.實現(xiàn)
@implementation NSMutableDictionary (NSMutableDictionaryEx)
/**
NSMutableDictionary 移除鍵值對
@param aKey 鍵值
*/
- (void)removeSafeObjectForKey:(id)aKey {
if (aKey) {
[self removeObjectForKey:aKey];
}
}
/**
NSMutableDictionary 添加新的鍵值對
@param anObject 值
@param aKey 鍵
*/
- (void)setSafeObject:(id)anObject forSafeKey:(id)aKey {
if (anObject && aKey) {
[self setObject:anObject forKey:aKey];
}
}
@end
六.最后
送上一張喜歡的圖片:

自由.jpeg
大家有興趣可以看一下,如果覺得不錯,麻煩給個喜歡或star,若發(fā)現(xiàn)問題請及時反饋,謝謝!