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