Git Product home page Git Product logo

easymodel's Introduction

easyModel

###简介 easyModel允许你通过JSON来构造数据对象,它会自动根据model中定义的property的名称来获取JSON字串中对应的key的值,简化了从JSON到数据构造的过程:

-(instancetype)initWithDic:(NSDictionary*)dic;
-(instancetype)initWithJson:(NSString*)jsonStr;
+(instancetype)modelWithDic:(NSDictionary*)dic;  
+(instancetype)modelWithJson:(NSString*)jsonStr;

###例子 我们一个 testType 继承于 easyModel 包含 常见的基础类型和列表其他easyModel对象,列表,常见于网络返回数据:

@interface testType:easyModel

@property(copy, nonatomic)  dispatch_block_t block;
@property(assign, nonatomic) BOOL b;
@property(assign, nonatomic) int i;
@property(assign, nonatomic) unsigned int ui;
@property(assign, nonatomic) NSInteger ni;
@property(assign, nonatomic) NSUInteger uni;
@property(assign, nonatomic) long l;
@property(assign, nonatomic) long long ll;
@property(assign, nonatomic) float f;
@property(assign, nonatomic) double d;
@property(copy, nonatomic) NSString *string;
@property(copy, nonatomic) NSMutableString *mstring;
@property(strong, nonatomic) NSNumber *number;
@property(strong, nonatomic) NSArray<NSString> *subStrings;
@property(strong, nonatomic)  NSArray<subTypeClass> *subTypes;
@property(strong, nonatomic)  NSMutableArray<subTypeClass> *msubTypes;

@end

###自定义字段解析 如果自己定义个一个model有个字段叫otherName 对应网络JSON返回的key为jsonName ,只需要重载initWithDic方法即可:

@implement testType

-(instancetype)initWithDic:(NSDictionary*)dic{

  self = [supper initWithDic:dic];

  if(self){

     self.otherName = [self getStringElementForKey:@"jsonName" dic:dic];

  }
  return self;
}
@end

###字段解析说明 现在easyModel还不支持NSDataNSDate,因为网络JSON字串不能返回data,返回的date或者是时间戳,或者是字符串,这个根据不同的项目定义都不一样,所以可以根据具体的项目使用自定义字段解析的方法来实现。

easymodel's People

Contributors

horacetao avatar wwf2123492 avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.