你的位置:首页 > 软件开发 > 操作系统 > 【疯狂造轮子

【疯狂造轮子

发布时间:2016-03-31 23:34:15
【疯狂造轮子-iOS】JSON转Model系列之一本文转载请注明出处 —— polobymulberry-博客园1. 前言之前一直看别人的源码,虽然对自己提升比较大,但毕竟不是自己写的,很容易遗忘。这段时间准备自己造一些轮子,主要目的还是为 ...

【疯狂造轮子

【疯狂造轮子-iOS】JSON转Model系列之一


本文转载请注明出处 —— polobymulberry-博客园

1. 前言


之前一直看别人的源码,虽然对自己提升比较大,但毕竟不是自己写的,很容易遗忘。这段时间准备自己造一些轮子,主要目的还是为了提升自身实力,总不能一遇到问题就Google【疯狂造轮子

之前写i博客园客户端的时候,经常会遇到JSON数据转Model的功能。一般遇到这种问题我都是自己在对应Model类中定义一个+ (instance)initWithAttributes:(NSDictionary *)attributes函数来将NSDictionary*数据转化为对应Model。

下面是i博客园中ICUser的部分代码,其中就使用了initWithAttributes。

// ICUser.h#import <Foundation/Foundation.h>extern NSString *const kUserId;extern NSString *const kUserBlogId;extern NSString *const kUserDisplayName;extern NSString *const kUserAvatarURL;@interface ICUser : NSObject@property (nonatomic, copy)   NSString  *userId;@property (nonatomic, assign)  NSInteger  blogId;@property (nonatomic, copy)   NSString  *displayName;@property (nonatomic, strong)  NSURL    *avatarURL;
+ (instancetype)initWithAttributes:(NSDictionary *
)attributes;@end// ICUser.m#import "ICUser.h"NSString *const kUserId         = @"UserId";NSString *const kUserBlogId       = @"BlogId";NSString *const kUserDisplayName    = @"DisplayName";NSString *const kUserAvatarURL     = @"Avatar";@implementation ICUser
+ (instancetype)initWithAttributes:(NSDictionary *
)attributes{  ICUser *user = [[ICUser alloc] init];
  user.userId       = attributes[kUserId];  user.blogId       = [attributes[kUserBlogId] integerValue];  user.displayName    = attributes[kUserDisplayName];  user.avatarURL     = [NSURL URLWithString:attributes[kUserAvatarURL]];return user;}@end

原标题:【疯狂造轮子

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录