zoukankan      html  css  js  c++  java
  • 第十四篇、常用的分类文件

    0.链接地址

    1.NSArray

    #import <Foundation/Foundation.h>
    
    @interface NSArray (Category)
    //以下写法均防止闪退
    + (instancetype)safeArrayWithObject:(id)object;
    
    - (id)safeObjectAtIndex:(NSUInteger)index;
    
    - (NSArray *)safeSubarrayWithRange:(NSRange)range;
    
    - (NSUInteger)safeIndexOfObject:(id)anObject;
    
    //通过Plist名取到Plist文件中的数组
    + (NSArray *)arrayNamed:(NSString *)name;
    
    // 数组转成json 字符串
    - (NSString *)toJSONStringForArray;
    @end
    #import "NSArray+Category.h"
    
    @implementation NSArray (Category)
    - (id)safeObjectAtIndexedSubscript:(NSUInteger)index
    {
        if (index >= self.count) {
            return nil;
        } else {
            return [self objectAtIndex:index];
        }
    }
    
    - (id)safeObjectAtIndex:(NSUInteger)index
    {
        if (index >= self.count) {
            return nil;
        } else {
            return [self objectAtIndex:index];
        }
    }
    
    + (instancetype)safeArrayWithObject:(id)object
    {
        if (object == nil) {
            return [self array];
        } else {
            return [self arrayWithObject:object];
        }
    }
    
    - (NSArray *)safeSubarrayWithRange:(NSRange)range
    {
        NSUInteger location = range.location;
        NSUInteger length = range.length;
        if (location + length > self.count) {
            //超过了边界,就获取从loction开始所有的item
            if ((location + length) > self.count) {
                length = (self.count - location);
                return [self safeSubarrayWithRange:NSMakeRange(location, length)];
            }
            
            return nil;
        }
        else {
            return [self subarrayWithRange:range];
        }
    }
    
    - (NSUInteger)safeIndexOfObject:(id)anObject
    {
        if (anObject == nil) {
            return NSNotFound;
        } else {
            return [self indexOfObject:anObject];
        }
    }
    
    + (NSArray *)arrayNamed:(NSString *)name
    {
        NSString *path = [[NSBundle mainBundle] pathForResource:name ofType:@"plist"];
        return [NSArray arrayWithContentsOfFile:path];
    }
    
    
    - (NSString *)toJSONStringForArray
    {
        NSData *paramsJSONData = [NSJSONSerialization dataWithJSONObject:self options:0 error:nil];
        return [[NSString alloc] initWithData:paramsJSONData encoding:NSUTF8StringEncoding];
    }
    @end


    2.NSDictionary

    #import <Foundation/Foundation.h>
    
    @interface NSDictionary (Category)
    
    //用于数据解析,返回对象为字符串或值类型,数组和字典不要用此方法
    - (id)safeObjectForKey:(NSString *)key;
    
    //设置键值对 针对对象为空处理
    - (void)safeSetObject:(id)object forKey:(id)key;
    - (id)objectForKeyCustom:(id)aKey;
    
    - (id)safeKeyForValue:(id)value;
    
    /**
     *  字段转成json的字符串
     *
     *  @return json 字符串
     */
    - (NSString *)toJSONStringForDictionary;
    @end
    #import "NSDictionary+Category.h"
    
    
    @implementation NSDictionary (Category)
    
    - (id)safeObjectForKey:(NSString *)key {
        if (key == nil || [self isKindOfClass:[NSNull class]]) {
            return nil;
        }
        id object = [self objectForKey:key];
        if (object==nil || object == [NSNull null]) {
            return @"";
        }
        return object;
    }
    
    
    - (void)safeSetObject:(id)object forKey:(id)key {
        if ([key isKindOfClass:[NSNull class]]) {
            return;
        }
        if ([object isKindOfClass:[NSNull class]]) {
            [self setValue:@"" forKey:key];
        }else{
            [self setValue:object forKey:key];
        }
    }
    
    - (id)objectForKeyCustom:(id)aKey {
        id object   = nil;
        
        // 检查是否字典对象
        if (![self isKindOfClass:[NSDictionary class]]) {
            return object;
        }
        
        // 保证key必须为字符串
        if (aKey && [aKey isKindOfClass:[NSString class]]) {
            object  = [self objectForKeyCustom:aKey];
        }
        
        return object;
    }
    
    - (id)safeKeyForValue:(id)value {
        for (id key in self.allKeys) {
            if ([value isEqual:[self objectForKey:key]]) {
                return key;
            }
        }
        return nil;
    }
    
    - (NSString *)toJSONStringForDictionary
    {
        NSData *paramsJSONData = [NSJSONSerialization dataWithJSONObject:self options:0 error:nil];
        return [[NSString alloc] initWithData:paramsJSONData encoding:NSUTF8StringEncoding];
    }
    
    @end
  • 相关阅读:
    《那些年啊,那些事——一个程序员的奋斗史》——72
    《那些年啊,那些事——一个程序员的奋斗史》——74
    《那些年啊,那些事——一个程序员的奋斗史》——71
    《那些年啊,那些事——一个程序员的奋斗史》——72
    boost asio 学习(二)了解boost::bind
    boost asio 学习(四)使用strand将任务排序
    boost asio 网络聊天 代码修改学习
    asio 广播代码示例
    c++ 博客资源
    boost::asio 学习草稿
  • 原文地址:https://www.cnblogs.com/HJQ2016/p/5811705.html
Copyright © 2011-2022 走看看