iOS

数组打印类

  • 创建数组分类NSArray+Log.h
  • .m文件重写方法descriptionWithLocale:

- (NSString *)descriptionWithLocale:(id)locale
{
    NSMutableString *string = [NSMutableString string];
    
    // 开头有个[
    [string appendString:@"[
"];
    
    // 遍历所有的元素
    [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        [string appendFormat:@"	%@,
", 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:@"{
"];
    
    // 遍历所有的键值对
    [self enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
        [string appendFormat:@"	%@", key];
        [string appendString:@" : "];
        [string appendFormat:@"%@,
", 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.

原文地址:https://www.cnblogs.com/adampei-bobo/p/6877110.html