溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

把請求的數(shù)據(jù)存放到Model中,(數(shù)組和和單條數(shù)據(jù))

發(fā)布時(shí)間:2020-06-15 12:06:08 來源:網(wǎng)絡(luò) 閱讀:1730 作者:Im劉亞芳 欄目:開發(fā)技術(shù)

1、Model存放到數(shù)組中


#import <Foundation/Foundation.h>
@interface VideoPinglunModel : NSObject
@property (nonatomic , assign)NSInteger commentUserid;
@property (nonatomic , assign)NSInteger commentCtime;
@property (nonatomic , copy)NSString *commentContent;
@property (nonatomic , copy)NSString *commentAuthor;
- (instancetype)initWithDictionary:(NSDictionary *)dic;
+ (NSMutableArray *)modelSformDics:(NSArray *)arr;
@end
- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{
//    if ([key isEqualToString:@"id"]) {
//        self.idM = value;
//    }
}
- (id)valueForUndefinedKey:(NSString *)key
{
    return nil;
}
- (instancetype)initWithDictionary:(NSDictionary *)dic
{
    self = [super init];
    if (self) {
        [self setValuesForKeysWithDictionary:dic];
    }
    return self;
}
+ (NSMutableArray *)modelSformDics:(NSArray *)arr
{
    NSMutableArray *modelArr = [NSMutableArray array];
    for (NSDictionary *dic in arr) {
        VideoPinglunModel *model = [[self alloc] initWithDictionary:dic];
        [modelArr addObject:model];
    }
    return modelArr;
}

使用方法;

//從解析的數(shù)組中查出需要的數(shù)據(jù),存放到類的字典里面

        NSMutableDictionary *dic = [responseObject objectForKey:@"data"];

        playingModel *model  = [[playingModel alloc] initWithDictionary:dic];



2、Model存放到中字典中(數(shù)據(jù)存到Model中)


#import <Foundation/Foundation.h>
@interface playingModel : NSObject
@property (nonatomic , copy)NSString *t;   //標(biāo)題吧 。。。
@property (nonatomic , copy)NSString *tag;
@property (nonatomic , copy)NSString *desc;
@property (nonatomic , copy)NSString *picpath;   //圖片地址
@property (nonatomic , copy)NSString *bigpicpath;
@property (nonatomic , assign)NSInteger vtime;
@property (nonatomic , copy)NSString *f;    //視頻播放地址
- (instancetype)initWithDictionary:(NSDictionary *)dic;
+ (instancetype)modelSformDics:(NSDictionary *)dic;
@end


#import "playingModel.h"
@implementation playingModel
- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{
    
}
- (id)valueForUndefinedKey:(NSString *)key
{
    return nil;
}
- (instancetype)initWithDictionary:(NSDictionary *)dic
{
    self = [super init];
    if (self) {
        [self setValuesForKeysWithDictionary:dic];
    }
    return self;
}
+ (instancetype)modelSformDics:(NSDictionary *)dic;
{
    playingModel *model = [[self alloc] initWithDictionary:dic];
    return model;
}
@end


/***************

 ****記住了*****

 **************/

            //判斷這個(gè)對象不為空的時(shí)候不為空的時(shí)候

            if (![[[responseObject objectForKey:@"data"] objectForKey:@"list"] isKindOfClass:[NSNull class]]) {

                NSMutableArray *arr = [[responseObject objectForKey:@"data"] objectForKey:@"list"];

                self.arrayComment = [VideoPinglunModel modelSformDics:arr];

            }

//字典編輯
+(id)getAUsefulInstanceWith:(NSDictionary *)attributes key:(NSString *)key{
    
    if ([[attributes objectForKey:key]
         isKindOfClass:[NSNumber class]] ||
        [[attributes objectForKey:key] isKindOfClass:[NSString class]] ||
        [[attributes objectForKey:key] isKindOfClass:[NSObject class]] || ![[attributes objectForKey:key] isKindOfClass:[NSNull class]])
    {
        return [NSMutableString stringWithFormat:@"%@", [attributes objectForKey:key]];
    }
    else
    {
        return @"";
        NSLog(@"字段值Id讀取異常(字段不存在或者值為空)");
    }
}


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI