1. mj_keyValues
将模型转化为字典。
Person *person = [[Person alloc] init];
person.name = @"John";
person.age = 22;
NSDictionary *dict = [person mj_keyValues];
2. mj_objectWithKeyValues
将字典转化为模型
NSDictionary *dict = @{@"name":@"John", @"age": @22};
Person *person = [Person mj_objectWithKeyValues:dict];
3. mj_keyValuesArrayWithObjectArray
将数组里的模型转化为字典数组。
NSArray *persons = @[person1, person2];
NSArray *dictArray = [Person mj_keyValuesArrayWithObjectArray:persons];
4. mj_objectArrayWithKeyValuesArray
将数组里的字典转化为模型数组。
NSArray *dictArray = @[@{@"name":@"John", @"age": @22}, @{@"name":@"Jane", @"age": @20}];
NSArray *persons = [Person mj_objectArrayWithKeyValuesArray:dictArray];
5. mj_replacedKeyFromPropertyName
属性名和字典名映射的方法,如果模型中的属性名和字典的key不一样,你可以使用这个方法来映射。
@implementation Person
+ (NSDictionary *)mj_replacedKeyFromPropertyName {
return @{@"personId" : @"id"}; // 将模型中的personId对应到字典中的id
return @{@"doge" : [Doge class]};
}
@end
6. mj_ignoredPropertyNames
用于忽略模型中的某些属性,不进行字典和模型的转换。
@implementation Person
+ (NSArray *)mj_ignoredPropertyNames {
return @[@"internalId"]; // 忽略'internalId'属性
}
@end
7. mj_objectClassInArray
在字典数组转模型数组的时候,告知数组中的字典应转为何种模型。
@implementation Person
+ (NSDictionary *)mj_objectClassInArray {
return @{@"friends" : [Friend class]}; // 字典数组'friends'对应的模型是'Friend'
}
@end
8. mj_setupNewValueFromOldValue
你可以使用这个方法来处理一些特殊的数据格式,通常在类的+load方法或者+initialize方法中调用。这样可以保证在模型被使用之前,已经设置好了转换规则,+initialize方法是Objective-C中的一个特殊方法,当类或其子类接收到第一条消息之前,运行时会自动调用这个方法。也就是说,这个方法是在类的第一个方法被调用之前调用的,我们通常利用这个方法来进行一些初始化设置。
@implementation Person
+ (void)initialize {
[Person mj_setupNewValueFromOldValue:^id(id object, id oldValue, MJProperty *property) {
// 如果属性是一个数组,并且我们希望将数组中的每个元素转换为整数
if ([property.name isEqualToString:@"arrayOfStrings"] && [oldValue isKindOfClass:[NSArray class]]) {
NSMutableArray *newArray = [NSMutableArray array];
for (NSString *numberString in oldValue) {
[newArray addObject:@([numberString intValue])];
}
return newArray;
}
// 如果属性是一个字典,我们想将它转换为一个自定义对象
else if ([property.name isEqualToString:@"dictionaryProperty"] && [oldValue isKindOfClass:[NSDictionary class]]) {
return [CustomObject mj_objectWithKeyValues:oldValue];
}
// 如果属性是一个字符串,我们想将它转换为NSURL
else if ([property.name isEqualToString:@"urlString"] && [oldValue isKindOfClass:[NSString class]]) {
return [NSURL URLWithString:oldValue];
}
// 如果属性是一个整数,我们想将它转换为NSNumber
else if ([property.name isEqualToString:@"integerProperty"] && [oldValue isKindOfClass:[NSString class]]) {
return @([oldValue integerValue]);
}
// 如果属性是一个JSON字符串,我们想将它转换为字典
else if ([property.name isEqualToString:@"jsonString"] && [oldValue isKindOfClass:[NSString class]]) {
NSData *jsonData = [oldValue dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil];
return jsonDict;
}
return oldValue;
}];
}
@end
9. mj_setupAllowedPropertyNames
这个方法让你指定哪些模型属性应该被包含在转换过程中。
+ (NSArray *)mj_ignoredPropertyNames {
return @[@"internalId"]; // 忽略'internalId'属性
}
10. copyWithZone
opyWithZone: 是 NSCopying 协议的方法,你需要在你的模型类中实现这个协议才能使用这个方法。MJExtension 提供了 mj_copy 方法,它可以帮助你轻松地实现深拷贝。
首先,你的模型类需要遵循 NSCopying 协议:
@interface CustomObject : NSObject<NSCopying>
@property (nonatomic, copy) NSString *property1;
@property (nonatomic, strong) NSMutableArray *property2;
// other properties
@end
@implementation CustomObject
+ (void)initialize {
[CustomObject mj_setupNewValueFromOldValue:^id(id object, id oldValue, MJProperty *property) {
if ([property.name isEqualToString:@"property2"] && [oldValue isKindOfClass:[NSMutableArray class]]) {
return [oldValue mutableCopy]; // 对可变数组进行深拷贝
}
return oldValue;
}];
}
- (id)copyWithZone:(NSZone *)zone {
return [self mj_copy];
}
@end
在这个例子中,我们假设Person对象有一个friends数组,它是一个NSMutableArray,我们想要对它进行深复制。copyItems:YES表示我们想要复制数组中的每个元素,而不仅仅是引用它们。
网友评论