65 lines
1.5 KiB
Objective-C
65 lines
1.5 KiB
Objective-C
//
|
|
// NSObject+MJExtension.m
|
|
// xplan-ios
|
|
//
|
|
// Created by zu on 2021/10/27.
|
|
//
|
|
|
|
#import "NSObject+MJExtension.h"
|
|
#import <MJExtension/MJExtension.h>
|
|
|
|
@implementation NSObject (MJExtension)
|
|
|
|
/// 依据数组初始化一个实例数组
|
|
/// @param json json 数据
|
|
+ (NSArray *)modelsWithArray:(id)json {
|
|
return [self mj_objectArrayWithKeyValuesArray:json];
|
|
}
|
|
|
|
/// 依据字典初始化一个实例
|
|
/// @param dictionary 字典
|
|
+ (instancetype)modelWithDictionary:(NSDictionary *)dictionary {
|
|
return [self mj_objectWithKeyValues:dictionary];
|
|
}
|
|
|
|
/// 依据JSON对象初始化一个实例
|
|
/// @param json json 数据
|
|
+ (instancetype)modelWithJSON:(id)json {
|
|
return [self mj_objectWithKeyValues:json];
|
|
}
|
|
|
|
///model 转字典
|
|
- (NSDictionary *)model2dictionary {
|
|
return [[self mj_keyValues] copy];
|
|
}
|
|
|
|
- (NSString *)toJSONString {
|
|
return [self mj_JSONString];
|
|
}
|
|
|
|
- (id)toJSONObject {
|
|
return [self mj_JSONObject];
|
|
}
|
|
|
|
///如果模型中包含数组的话
|
|
+ (NSDictionary *)mj_objectClassInArray {
|
|
return [self objectClassInArray];
|
|
}
|
|
|
|
///模型中需要映射 重写整个方法
|
|
+ (NSDictionary *)mj_replacedKeyFromPropertyName {
|
|
return [self replacedKeyFromPropertyName];
|
|
}
|
|
|
|
///扩展方法 按需索取 重写即可
|
|
///如果一个模型中 包含一个数组 数组中是另一个模型
|
|
+ (NSDictionary *)objectClassInArray {
|
|
return @{};
|
|
}
|
|
///如果一个模型中需要字段映射的话 比如id -> ID name -> other.name
|
|
+ (NSDictionary *)replacedKeyFromPropertyName {
|
|
return @{};
|
|
}
|
|
|
|
@end
|