Giter VIP home page Giter VIP logo

zkmodel's Introduction

ZKModel

Build Status license CocoaPods platform platform

ZKModel 是一个实体模型转换器

安装

手动安装

拷贝 ZKModel 目录到项目里即可。

CocoaPods

pod 'ZKModel'

使用

实体类:

UserModel.h

#import "ZKModel.h"

@interface UserModel : ZKModel<ZKModelSerializing>
/**
 *  对应数据表字段id,对应json字段id
 */
@property(nonatomic, copy) NSString *key;
/**
 *  对应数据表字段name,对应json字段name
 */
@property(nonatomic, copy) NSString *name;
/**
 *  对应数据表字段age,对应json字段
 */
@property(nonatomic, copy) NSNumber *age;
@end

UserModel.m

#import "UserModel.h"

@implementation UserModel

+ (NSString *)ZKDBTableName {
    return @"User";
}
+ (NSDictionary *)ZKDBPrimaryKeys {
    return @{ @"key" : @"id" };
}
+ (NSDictionary *)ZKPropertyKeys {
    return @{
             @"key" : @"id",
             @"name" : @"name",
             @"age" : @"age"
             };
}
@end

###示例

    NSDictionary *userDic = @{ @"id" : @"1", @"name" : @"王文壮", @"age" : @30 };
    // 将 NSDictionary 转换为 UserModel
    UserModel *user = [ZKModelAdapter modelOfClass:[UserModel class] fromDictionary:userDic];
    
    NSArray *userArray = @[
                           @{ @"id" : @"1", @"name" : @"王文壮", @"age" : @30 },
                           @{ @"id" : @"2", @"name" : @"壮文王", @"age" : @30 }
                           ];
    // 将元素为 NSDictionary 的 NSArray 转换为元素为 UserModel 的 NSArray
    NSArray *users = [ZKModelAdapter modelsOfClass:[UserModel class] fromArray:userArray];
    
    // 将 UserModel 转换为 JSON
    NSString *json = [ZKModelAdapter JSONOfModelClass:[UserModel class] fromModel:user];
    NSLog(@"User JSON:%@", json);
    
    // 将元素为 UserModel 的 NSArray 转换为 JSON
    json = [ZKModelAdapter JSONOfModelArray:users];
    NSLog(@"Users JSON:%@", json);
    
    // 实体输出 JSON
    json = [user toJSON];
    NSLog(@"Users JSON:%@", json);
    
    // 实体输出 NSDictionary
    userDic = [user dictionry];

运行环境

  • iOS 7+
  • 支持 armv7/armv7s/arm64

zkmodel's People

Contributors

wangwenzhuang avatar

Watchers

James Cloos avatar  avatar

Forkers

bjdfwz

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.