XTYModel 1.2.0

XTYModel 1.2.0

测试测试过
语言语言 Obj-CObjective C
许可协议 MIT
发布最后发布2016年9月

Michael维护。



XTYModel 1.2.0

  • 作者:
  • Michael

适用于 iOS 的一种轻量级、高性能模型框架。

主要功能

  • 支持所有必要的最基本对象,并且对象类型可以自动转换。
  • 在转换过程中将验证所有数据类型以确保类型安全。
  • 模型类不继承自任何其他基类。
  • 此库仅包含4个文件。

要求

  • iOS 7.0+
  • Xcode 7.11+

安装

框架

  • 将 XTYModel.framework 添加到您的项目中
  • 导入 "XTYModelItem.h"

API

XTYJson

/** jsonObj is NSDictionary or NSArray*/
@property(nonatomic, readonly) id jsonObj;

/**  init method*/
- (instancetype)initWithObject:(id)object;
+ (XTYJson *)jsonWithObject:(id)obj;
+ (XTYJson *)jsonWithData:(NSData*)jsonData;
+ (XTYJson *)jsonWithString:(NSString*)jsonStr;

/**  get json value with key*/
- (XTYJson*)jsonForKey:(NSString*)key;

/** get json value with index*/
- (XTYJson*)jsonAtIndex:(NSUInteger)index;

XTYModelItem


/**
 *  the raw data properties, readonly. When we parse the properties, the item retains the original data, this data can use to upload to the server or anyother uses.
 */
@property (nonatomic, readonly) NSDictionary* originDict;
@property (nonatomic, readonly) XTYJson *json;

/**
 *  init method, the input data is a XTYJson instance, if your original data is a dictionary, just use the XTYJson init method to get a instance with your dictionary. We do not encourage to use original data class, like NSDictionary or NSArray to init item.
 */
- (instancetype)initWithJson:(XTYJson*)json;
- (instancetype)initwithDictionary:(NSDictionary *)dictionary;

/**
 *  follow methods use to parse list data structure, they are class method and should be used with subClass
 */
+ (NSArray<__kindof XTYModelItem *> *)arrayWithArrayDictionary:(NSArray<NSDictionary *>*)arrayDict;
+ (NSMutableArray<__kindof XTYModelItem *> *)mutableArrayWithArrayDictionary:(NSArray<NSDictionary *>*)arrayDict;

#pragma mark - config methods,rewrite them in the subClass if you need
/**
 *  the map between json key and property key <NSString* -> NSString*>, if the key return from the server is not same as the property name, you can rewrite this method to make the properties parse correctly
 */
+ (NSDictionary *)JSONKeyMapForProperties;

/**
 *  returen a dictionary, if the property is kind of NSArray instance or inherite from it, the key is the property name<NSString *>, the value is the Class <like [NSString class]>
 */
+ (NSDictionary *)elementClassMapForNSArrayProperties;

用法

将模型属性与不同的 JSON 键匹配

// json data - gitHub_user
{
    "login": "facebook",
    "userID": 69631,
    "avatar_url": "https://avatars.githubusercontent.com/u/69631?v=3",
    "gravatar_id": "",
    "url": "https://api.github.com/users/facebook",
    "html_url": "https://github.com/facebook",
    "followers_list": [
                       {
                       "userID":777,
                       "url":"https://api.github.com/users/facebook/followers"
                       },
                       {
                       "userID":777,
                       "url":"https://api.github.com/users/facebook/followers"
                       }
                       ,
                       {
                       "userID":777,
                       "url":"https://api.github.com/users/facebook/followers"
                       }
                    ],
    "following_url": "https://api.github.com/users/facebook/following{/other_user}
    "public_repos": 147,
    "public_gists": 12,
    "followers": 3,
    "following": 0
}

模型项

@interface XTYGitHubFollower : XTYModelItem

@property (nonatomic, assign) NSInteger userID;
@property (nonatomic, strong) NSString *url;

@end

@interface XTYGitHubUserItem : XTYModelItem

@property (nonatomic, strong) NSString *login;
@property (nonatomic, assign) NSInteger userID;
@property (nonatomic, strong) NSString *avatar_url;
@property (nonatomic, strong) NSString *gravatar_id;
@property (nonatomic, strong) NSString *url;
@property (nonatomic, strong) NSString *html_url;
@property (nonatomic, strong) NSArray<XTYGitHubFollower *> *followers_list;
@property (nonatomic, strong) NSString *following_url;
@property (nonatomic, assign) NSInteger publicRepos;
@property (nonatomic, assign) NSInteger publicGists;
@property (nonatomic, assign) NSInteger followers;
@property (nonatomic, assign) NSInteger following;

@end
  • 注意,followers_list 是一个包含 XTYGitHubFollower 实例的数组,并且 publicRepospublicGists 与 JSON 键不相等。因此,我们应该通过重写超类方法进行一些映射操作
@implementation XTYGitHubUserItem

+ (NSDictionary *)JSONKeyMapForProperties
{
    return @{@"publicRepos":@"public_repos",
             @"publicGists":@"public_gists"};
}

+ (NSDictionary *)elementClassMapForNSArrayProperties
{
    return @{@"followers_list":[XTYGitHubFollower class]};
}

@end

使用 XTYGitHubUserItem 解析 JSON 数据

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"gitHub_user" ofType:@"json"];
XTYJson *git_user_json = [XTYJson jsonWithData:[NSData dataWithContentsOfFile:filePath]];
XTYGitHubUserItem *userItem = [[XTYGitHubUserItem alloc] initWithJson:git_user_json];

许可协议

XTYModel 在 MIT 许可协议下发布。有关详细信息,请参阅 LICENSE。