当前位置: 首页 > 工具软件 > Mantle > 使用案例 >

iOS mantle 使用

傅阿苏
2023-12-01

model中带数组的情况

#import <Mantle/Mantle.h>
#import "MantleModelCommonHeader.h"
#import "MessageModel.h"


@interface MessageListModel : MTLModel <MTLJSONSerializing>

@property (nonatomic, strong) NSMutableArray *messageArray; // 赞数组
@property (nonatomic, strong) NSString *lastId; // 最后一条的ID

@end



#import "MessageListModel.h"

@implementation MessageListModel


/**
 * 将JSON字典中键对应的值,赋值给属性值
 */
+ (NSDictionary *)JSONKeyPathsByPropertyKey{
    return @{
             //前面是属性,后面是JSON的key
             @"messageArray": @"list",
             @"lastId": @"last_id",
             };
}



#pragma mark - 特殊字段的处理方法回调函数


+ (NSValueTransformer *)messageArrayJSONTransformer{
    
    return [MTLValueTransformer transformerUsingForwardBlock:^id(id value, BOOL *success, NSError *__autoreleasing *error) {
        NSArray *jsonArray = value;
        NSMutableArray *messageArray = [NSMutableArray array];
        
        [jsonArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSError *error = nil;
            
            
            MessageModel *messageModel = [MTLJSONAdapter modelOfClass:[MessageModel class] fromJSONDictionary:obj error:&error];
            if (messageModel && messageModel.messageUser) {
                [messageArray addObject:messageModel];
            }
        }];
        
        
        return messageArray;
    }];
    
}



@end

model中带复合model的情况



/**
 *  消息模块基础的数据模型
 */
@interface MessageModel : MTLModel <MTLJSONSerializing>


@property (nonatomic, strong) NSString *messageId;
@property (nonatomic, strong) NSString *messageType;
@property (nonatomic, strong) NSString *userId;
@property (nonatomic, strong) NSString *messageUserId;
@property (nonatomic, strong) NSString *messageMediaId;
@property (nonatomic, strong) NSString *messageContent;
@property (nonatomic, strong) NSString *messageStatus;
@property (nonatomic, strong) NSString *messageCreateTime;
@property (nonatomic, strong) NSString *messageScheme;
@property (nonatomic, strong) NSString *messageCommentId;

@property (nonatomic, strong) MessageUserModel *messageUser;
@property (nonatomic, strong) MessageMediaModel *messageMedia;


@end


#import "MessageModel.h"

@implementation MessageModel


/**
 * 将JSON字典中键对应的值,赋值给属性值
 */
+ (NSDictionary *)JSONKeyPathsByPropertyKey{
    return @{
             //前面是属性,后面是JSON的key
             @"messageId": @"message_id",
             @"messageType": @"type",
             @"userId": @"user_id",
             @"messageUserId": @"message_user_id",
             @"messageMediaId": @"media_id",
             @"messageContent": @"content",
             @"messageStatus": @"status",
             @"messageCommentId": @"comment_id",
             @"messageCreateTime": @"created_time",
             @"messageUser": @"message_user",
             @"messageMedia": @"media",
             @"messageScheme":@"scheme"
             };
}

#pragma mark - UserModel解析
/**
 * media的解析
 */
+ (NSValueTransformer *)messageUserJSONTransformer{
    return [MTLValueTransformer transformerUsingForwardBlock:^id(id value, BOOL *success, NSError *__autoreleasing *error) {
        
        NSError *errorMessageUserModel = nil;
        MessageUserModel *messageUserModel = [MTLJSONAdapter modelOfClass:[MessageUserModel class] fromJSONDictionary:value error:&errorMessageUserModel];
        
        return messageUserModel;
        
    }];
}


/**
 * praiseUser的解析
 */
+ (NSValueTransformer *)messageMediaJSONTransformer{
    return [MTLValueTransformer transformerUsingForwardBlock:^id(id value, BOOL *success, NSError *__autoreleasing *error) {
        
        NSError *errorMessageMedia = nil;
        MessageMediaModel *messageMediaModel = [MTLJSONAdapter modelOfClass:[MessageMediaModel class] fromJSONDictionary:value error:&errorMessageMedia];
        
        return messageMediaModel;
        
    }];
}



@end

 

 类似资料: