• 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.

  • 相关阅读:
    python可视化---plot()函数
    蜂鸣器的相关知识及简单应用
    将一个文件夹中多个视频的视频帧保存在多个文件夹下
    键盘按键控制程序的简单案例
    Tkinter模块的详细总结
    控制LED灯发光
    python socket 套接字编程 单进程服务器 实现多客户端访问
    python 报错RuntimeError: dictionary changed size during iteration
    HTTP请求行、请求头、请求体详解(转)
    python UDP套接字通信
  • 原文地址:https://www.cnblogs.com/adampei-bobo/p/6877110.html
Copyright © 2020-2023  润新知