UI23_基类

基类, 不是鸡肋
ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController


@end

ViewController.m

#import "ViewController.h"
#import "Test.h"
#import "AFNetworking.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self createData];


}

- (void)createData {
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@"http://lib.wap.zol.com.cn/ipj/docList/?v=3.0&class_id=0&page=1&retina=1&last_time=2015-10-09%2017:20&vs=iph430&isReviewing=NO%22" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        NSMutableArray *arr = [Test baseModelByArr:dic[@"list"]];
        for (Test *test in arr) {
            NSLog(@"%@", test.stitle);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

    }];
}

Test.h

#import "BaseModel.h"

@interface Test : BaseModel
@property(nonatomic, copy)NSString *stitle;

@end

Test.m

#import "Test.h"

@implementation Test

@end

BaseModel.h

#import <Foundation/Foundation.h>

@interface BaseModel : NSObject

//  把数组套字典的传给方法, 返回一个数组套model
+ (NSMutableArray *)baseModelByArr:(NSArray *)arr;

@end

BaseModel.m

#import "BaseModel.h"

@implementation BaseModel

+ (NSMutableArray *)baseModelByArr:(NSArray *)arr {
    //  先初始化容器arr
    NSMutableArray *modelArr = [NSMutableArray array];
    //  遍历数组
    for (NSDictionary *dic in arr) {
        @autoreleasepool {

            //   通过便利构造器来创建对象
            id model = [[self class] baseModelWithDic:dic];
            [modelArr addObject:model];

        }


    }
    return modelArr;
}

+ (instancetype)baseModelWithDic:(NSDictionary *)dic {
    //  通过多态创建对象
    id model = [[[[self class] alloc] initWithDic:dic] autorelease];

    return model;
}

- (instancetype)initWithDic:(NSDictionary *)dic {
    self = [super init];
    if (self) {
        //  进行KVC的赋值
        [self setValuesForKeysWithDictionary:dic];
    }
    return self;
}

- (void)setValue:(id)value forUndefinedKey:(NSString *)key {

}


@end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值