您的位置:首页 > 移动开发 > IOS开发

iOS - 数组,字典打印以及防止数组取值越界字典插入nil

2017-05-19 10:37 1311 查看

数组打印类

创建数组分类
NSArray+Log.h


.m文件重写方法
descriptionWithLocale:


- (NSString *)descriptionWithLocale:(id)locale
{
NSMutableString *string = [NSMutableString string];

// 开头有个[
[string appendString:@"[\n"];

// 遍历所有的元素
[self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[string appendFormat:@"\t%@,\n", obj];
}];

// 结尾有个]
[string appendString:@"]"];

// 查找最后一个逗号
NSRange range = [string rangeOfString:@"," options:NSBackwardsSearch];
if (range.location != NSNotFound)
[string deleteCharactersInRange:range];

return string;
}

字典打印类

创建字典分类
NSDictionary+Log.h


.m文件重写方法
descriptionWithLocale


-(NSString *)descriptionWithLocale:(id)locale{

NSMutableString *string = [NSMutableString string];
// 开头有个{
[string appendString:@"{\n"];

// 遍历所有的键值对
[self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
[string appendFormat:@"\t%@", key];
[string appendString:@" : "];
[string appendFormat:@"%@,\n", obj];
}];
// 结尾有个}
[string appendString:@"}"];
// 查找最后一个逗号
NSRange range = [string rangeOfString:@"," options:NSBackwardsSearch];
if (range.location != NSNotFound)
[string deleteCharactersInRange:range];

return string;
}

防止数组取值越界

创建数组分类
NSArray+EM


.m文件通过
runtime
交换方法为自己写的
em_objectAtIndex
取值方法,从中判断越界情况

#import "NSArray+EM.h"
#import <objc/runtime.h>
@implementation NSArray (EM)
+(void)load {
Method fromMethod = class_getInstanceMethod(objc_getClass("__NSArrayI"), @selector(objectAtIndex:));
Method toMethod = class_getInstanceMethod(objc_getClass("__NSArrayI"), @selector(em_objectAtIndex:));
method_exchangeImplementations(fromMethod, toMethod);
}

- (id)em_objectAtIndex:(NSUInteger)index {
if (self.count - 1 < index) {
return @"越界";
}else {
return [self em_objectAtIndex:index];
}
}
@end


可变数组分类
NSMutableArray+EM
类似

#import "NSMutableArray+EM.h"
#import <objc/runtime.h>
@implementation NSMutableArray (EM)
+(void)load {
Method fromMethod = class_getInstanceMethod(objc_getClass("__NSArrayM"), @selector(objectAtIndex:));
Method toMethod = class_getInstanceMethod(objc_getClass("__NSArrayM"), @selector(em_objectAtIndex:));
method_exchangeImplementations(fromMethod, toMethod);
}

- (id)em_objectAtIndex:(NSUInteger)index {
if (self.count - 1 < index) {
return @"越界";
}else {
return [self em_objectAtIndex:index];
}
}
@end

可变字典防止插入空nil

创建分类
NSMutableDictionary+EM


.m文件

#import "NSMutableDictionary+EM.h"
#import <objc/runtime.h>
@implementation NSMutableDictionary (EM)
+ (void)load {

Method fromMethod = class_getInstanceMethod(objc_getClass("__NSDictionaryM"), @selector(setObject:forKey:));
Method toMethod = class_getInstanceMethod(objc_getClass("__NSDictionaryM"), @selector(em_setObject:forKey:));
method_exchangeImplementations(fromMethod, toMethod);
}

- (void)em_setObject:(id)emObject forKey:(NSString *)key {
if (emObject == nil) {

[self em_setObject:@"字典插入了nil" forKey:key];
}else {
[self em_setObject:emObject forKey:key];
}
}
@end


测试打印

字典中
hello
对应的object插入了nil,则
hello
对应的value被我们替换成了
NSMutableDictionary+EM.m
文件中的自定义的语句

数组取值越界同样返回了被我们定义的内容
越界





项目中经常遇到的就是
tableView
给cell赋值数组取值越界,以及发送网络请求字典中插入了空值,在返回的数据中用YYModel解决返回nil.

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐