UI23_基类

来源:互联网 发布:服务器监控软件免费版 编辑:程序博客网 时间:2024/05/12 02:45

基类, 不是鸡肋
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
1 0
原创粉丝点击