Giter VIP home page Giter VIP logo

ssobject's Introduction

SSObject

Pod Version Pod Platform License

Quick start

SSObject 支持 CocoaPods. 添加下面的配置到Podfile:

pod 'SSObject'

功能:

  • 可以将数组或键值对映射成本地对象,主要用于对象序列化和网络数据解析
  • 数据模型和字典互转

常用场景:

  • 网络请求Json数据 --> 解析Json成本地数组或字典 --> 使用本类将数组或字典映射为数据模型
  • 对象序列化成字典保存

特性:

  • 实现对象正向反向映射, 自动判断属性类型,使用字典对应的键值匹配属性名进行初始化赋值
  • 支持类型 NSString, NSDate, NSNumber, NSArray, 基本常量, SSObject及其子类
  • 对于NSDate,如果是数字自动按照时间戳进行转换, 如果是字符串,按照提供的时间格式转换
  • 支持数组直接序列化成对象!

注意:

  • 如果只读属性实现了Get方法,则会抛出异常,虽然内部已经实现捕获异常不会导致崩溃,但是建议使用时注意

用例:

数据模型类实现:

@interface SimpleObject : SSObject
@property (nonatomic, strong) SimpleObject * testObj;
@property (nonatomic, strong) NSString * name;
@property (nonatomic, assign) int intValue;
@property (nonatomic, strong) NSArray * testItems;
@property (nonatomic, strong) id value;
@end
@implementation SimpleObject
- (Class)arrayClassWithPropertyName:(NSString *)propertyName {
    return SimpleObject.class;
}
@end

实例化:

NSDictionary * dic = @{ @"testObj": @{  @"name": @"test2",
                                            @"intValue": @(80) },
                            @"name":        @"test1",
                            @"intValue":    @"10",
                            @"value":       @{  @"key1": @"value1" },
                            @"testItems":   @[  @{ @"name": @"testName1"},
                                                @{ @"name": @"testName2"},
                                                @{ @"name": @"testName3"}]};
NSArray * arr = @[  @{@"name": @"testName1"},
                    @{@"name": @"testName2"},
                    @{@"name": @"testName3"}];
SimpleObject * obj = [SimpleObject objectWithDictionary:dic];
NSArray * objItems = [SimpleObject arrayWithDictionarys:arr];
NSLog(@"%@", obj);
NSLog(@"%@", obj.dictionaryFormInfo);
NSLog(@"%@", objItems);

ssobject's People

Contributors

qzs21 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  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.