iOS - 开发技巧

iOS - 基于MJE的数据处理封装

2018-01-11  本文已影响8人  SkyMing一C

Demo_gitHub

图片源于网络

基于MJE的数据处理封装

#import <Foundation/Foundation.h>

typedef NS_ENUM(NSInteger,SkyDataType) {
    SkyDataTypeDictionary = 0,//最外层是字典
    SkyDataTypeArray, //最外层是数组
    SkyDataTypeNone //是字符串
};

@interface DataModelManager : NSObject

/**
 处理data数据
 @param responseObject 原始数据
 @param dataType 数据最外层类型
 @param modelName 模型名字
 @param modelKey 解析关键字
 @return 处理完的数据数组
 */
+(NSMutableArray *)dataDealWithResponseObject:(id)responseObject dataType:(SkyDataType)dataType modelName:(NSString *)modelName modelKey:(NSString *)modelKey;
@end

#import "DataModelManager.h"
#import "MJExtension.h"

@interface DataModelManager()
+(instancetype)sharedAFManager;
@end
@implementation DataModelManager
static DataModelManager *aManager;
+ (DataModelManager *)sharedAFManager
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        aManager =  [[[super class] alloc]init];
    });
    return aManager;
}
+(NSMutableArray *)dataDealWithResponseObject:(id)responseObject dataType:(SkyDataType)dataType modelName:(NSString *)modelName modelKey:(NSString *)modelKey
{
    //处理二进制数据
    NSString *biteObject = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
    //过滤无用字符
    biteObject = [[self sharedAFManager] filtrationString:biteObject deleteStringArray:@[@"\r\n",@"\n",@"\t"]];
    //整理
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    if (!skyStringIsEmpty(biteObject)) {
        
        if (dataType == SkyDataTypeNone) {
            //是字符串
           dic[modelName] = @[biteObject];
        }
        
        if (dataType == SkyDataTypeDictionary) {
            NSData *data = [biteObject dataUsingEncoding:NSUTF8StringEncoding];
            //最外层是字典
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            [dic addEntriesFromDictionary:dict];
        }
        
        if (dataType == SkyDataTypeArray) {
            
            NSData *data = [biteObject dataUsingEncoding:NSUTF8StringEncoding];
            //最外层是数组
            NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            dic[modelName] = array;
        }
    }
    //解析
    NSMutableArray *modelArray = [NSMutableArray array];
    //是字符串
    if (dataType == SkyDataTypeNone) {
        [modelArray addObjectsFromArray:dic[modelName]];
    }
    //最外层是字典
    if (dataType == SkyDataTypeDictionary) {
        [modelArray addObjectsFromArray:[NSClassFromString(modelName) mj_objectArrayWithKeyValuesArray:dic[modelKey]]];
    }
    //最外层是数组
    if (dataType == SkyDataTypeArray) {
        [modelArray addObjectsFromArray:[NSClassFromString(modelName) mj_objectArrayWithKeyValuesArray:dic[modelName]]];
    }
    
    
    return modelArray;
}

#pragma mark --- 去除字符
-(NSString *)filtrationString:(NSString *)String deleteStringArray:(NSArray *)deleteStringArray
{
    if (String == NULL || [String isEqualToString:@""]) {
        return @"";
    }
    NSMutableString *str1 = [NSMutableString stringWithString:String];
    for (int i = 0; i<deleteStringArray.count; i++) {
        NSRange range = [str1 rangeOfString:deleteStringArray[i]];
        while (range.location != NSNotFound) {
            [str1 deleteCharactersInRange:range];
            range = [str1 rangeOfString:deleteStringArray[i]];
        }
    }
    return str1;
    
}
@end

主要步骤

typedef NS_ENUM(NSInteger,SkyDataType) {
    SkyDataTypeDictionary = 0,//最外层是字典
    SkyDataTypeArray, //最外层是数组
    SkyDataTypeNone //是字符串
};
@interface DataModelManager()
+(instancetype)sharedAFManager;
@end
@implementation DataModelManager
static DataModelManager *aManager;
+ (DataModelManager *)sharedAFManager
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        aManager =  [[[super class] alloc]init];
    });
    return aManager;
}
@end
    //处理二进制数据
    NSString *biteObject = [[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
    //过滤无用字符
    biteObject = [[self sharedAFManager] filtrationString:biteObject deleteStringArray:@[@"\r\n",@"\n",@"\t"]];
#pragma mark --- 去除字符
-(NSString *)filtrationString:(NSString *)String deleteStringArray:(NSArray *)deleteStringArray
{
    if (String == NULL || [String isEqualToString:@""]) {
        return @"";
    }
    NSMutableString *str1 = [NSMutableString stringWithString:String];
    for (int i = 0; i<deleteStringArray.count; i++) {
        NSRange range = [str1 rangeOfString:deleteStringArray[i]];
        while (range.location != NSNotFound) {
            [str1 deleteCharactersInRange:range];
            range = [str1 rangeOfString:deleteStringArray[i]];
        }
    }
    return str1;
    
}
    //整理
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    if (!skyStringIsEmpty(biteObject)) {
        
        if (dataType == SkyDataTypeNone) {
            //是字符串
           dic[modelName] = @[biteObject];
        }
        
        if (dataType == SkyDataTypeDictionary) {
            NSData *data = [biteObject dataUsingEncoding:NSUTF8StringEncoding];
            //最外层是字典
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            [dic addEntriesFromDictionary:dict];
        }
        
        if (dataType == SkyDataTypeArray) {
            
            NSData *data = [biteObject dataUsingEncoding:NSUTF8StringEncoding];
            //最外层是数组
            NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            dic[modelName] = array;
        }
    }
    //解析
    NSMutableArray *modelArray = [NSMutableArray array];
    //是字符串
    if (dataType == SkyDataTypeNone) {
        [modelArray addObjectsFromArray:dic[modelName]];
    }
    //最外层是字典
    if (dataType == SkyDataTypeDictionary) {
        [modelArray addObjectsFromArray:[NSClassFromString(modelName) mj_objectArrayWithKeyValuesArray:dic[modelKey]]];
    }
    //最外层是数组
    if (dataType == SkyDataTypeArray) {
        [modelArray addObjectsFromArray:[NSClassFromString(modelName) mj_objectArrayWithKeyValuesArray:dic[modelName]]];
    }

Demo_gitHub

上一篇下一篇

猜你喜欢

热点阅读