字典转模型的三种方法之一:赋值法
1 #import "YSHomeViewController.h" 2 #import "UIImage+YS.h" 3 #import "UIBarButtonItem+YS.h" 4 #import "YStitleButton.h" 5 #import "AFNetworking.h" 6 #import "YSaccountTool.h" 7 #import "YSaccount.h" 8 #import "UIImageView+WebCache.h" 9 #import "YSStatus.h" 10 #import "YSUser.h" 11 @interface YShomeViewController() 12 @property(nonatomic,strong)NSArray *statuses; 13 @end 14 @implementation YShomeViewController 15 16 -(void)viewDidLoad 17 { 18 [super viewDidLoad]; 19 [self setUpNavBar]; 20 [self setUpWeiboList]; 21 22 }
1 -(void)setUpWeiboList 2 { 3 AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager]; 4 NSMutableDictionary *param = [NSMutableDictionary dictionary]; 5 YSaccount *account = [YSaccountTool account]; 6 7 param[@"access_token"] = account.access_token; 8 // param[@"count"] = @1; 9 [mgr GET:@"https://api.weibo.com/2/statuses/home_timeline.json" parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) { 10 //这里的responseObject为json格式的返回结果,http://open.weibo.com/wiki/2/statuses/home_timeline,会被AFN处理为字典. 11 NSArray *dictArray = responseObject[@"statuses"]; 12 NSMutableArray *MArray = [NSMutableArray array]; 13 for (NSDictionary *dict in dictArray) { 14 YSStatus *status = [YSStatus statusWithDict:dict]; 15 [MArray addObject:status]; 16 } 17 self.statuses = MArray; 18 [self.tableView reloadData]; 19 } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 20 21 }]; 22 }
1 //#import <Foundation/Foundation.h> 2 //@class YSUser; 3 //@interface YSStatus : NSObject 4 ///** 5 // * 微博的内容(文字) 6 // */ 7 //@property (nonatomic, copy) NSString *text; 8 ///** 9 // * 微博的来源 10 // */ 11 //@property (nonatomic, copy) NSString *source; 12 ///** 13 // * 微博的ID 14 // */ 15 //@property (nonatomic, copy) NSString *idstr; 16 ///** 17 // * 微博的转发数 18 // */ 19 //@property (nonatomic, assign) int reposts_count; 20 ///** 21 // * 微博的评论数 22 // */ 23 //@property (nonatomic, assign) int comments_count; 24 ///** 25 // * 微博的作者 26 // */ 27 //@property (nonatomic, strong) YSUser *user; 28 // 29 //+(instancetype)statusWithDict:(NSDictionary *)dict; 30 // 31 //-(instancetype)initWithDict:(NSDictionary *)dict; 32 //@end 33 #import "YSStatus.h" 34 #import "YSUser.h" 35 @implementation YSStatus 36 37 +(instancetype)statusWithDict:(NSDictionary *)dict 38 { 39 return [[self alloc]initWithDict:dict]; 40 } 41 42 -(instancetype)initWithDict:(NSDictionary *)dict 43 { 44 if (self = [super init]) { 45 self.idstr = dict[@"idstr"]; 46 self.text = dict[@"text"]; 47 self.source = dict[@"source"]; 48 self.reposts_count = [dict[@"reposts_count"] intValue]; 49 self.comments_count = [dict[@"comments_count"] intValue]; 50 51 self.user = [YSUser userWithDict:dict[@"user"]]; 52 } 53 return self; 54 } 55 @end
1 //#import <Foundation/Foundation.h> 2 // 3 //@interface YSUser : NSObject 4 ///** 5 // * 用户的ID 6 // */ 7 //@property (nonatomic, copy) NSString *idstr; 8 ///** 9 // * 用户的昵称 10 // */ 11 //@property (nonatomic, copy) NSString *name; 12 ///** 13 // * 用户的头像 14 // */ 15 //@property (nonatomic, copy) NSString *profile_image_url; 16 // 17 // 18 //+(instancetype)userWithDict:(NSDictionary *)dict; 19 // 20 //-(instancetype)initWithDict:(NSDictionary *)dict; 21 //@end 22 23 24 #import "YSUser.h" 25 26 @implementation YSUser 27 28 +(instancetype)userWithDict:(NSDictionary *)dict 29 { 30 return [[self alloc]initWithDict:dict]; 31 } 32 33 -(instancetype)initWithDict:(NSDictionary *)dict 34 { 35 if (self = [super init]) { 36 self.idstr = dict[@"idstr"]; 37 self.name = dict[@"name"]; 38 self.profile_image_url = dict[@"profile_image_url"]; 39 } 40 return self; 41 } 42 @end