HPMapper 0.0.5

HPMapper 0.0.5

测试已测试
Lang语言 Obj-CObjective C
许可证 MIT
Released上次发布2017年4月

Huy Pham 维护。



HPMapper 0.0.5

原因

我知道很多开源软件都有 JSON 反序列化功能,但您仍需要手动实现令人厌烦的映射函数。为了解决这个问题,我创建了 Mapper,使用 Objective-C 运行时,这样您就不再需要这样做。太棒了。

Mapper 可以与 Swift 对象(例如)集成,将 JSON 映射到对象,并将对象映射到 JSON,而无需手动实现。它非常简单易用。

安装

  pod "HPMapper"

工作原理

创建模型

    // Create model inheritance Mapper class and that's all
    // Objective-C (Recommended)
    #import "Mapper.h"

    @interface BaseModel : Mapper
        @property (nonatomic, copy) NSString *ID;
        @property (nonatomic, copy) NSString *Name;
    @end

    // Or Swift
    class Carrot: Mapper {  
      dynamic var ID: NSString!
      dynamic var Name: NSString!
    }

    let model = Model()   
   // Init it with json
    model.initData(["Name": "Carrot", "ID": "A2jsdk"])

   // Or simple
    let model = Model(dictionary: ["Name": "This name", "ID": "This is ID from super class"])

有时您需要将其转换回 JSON

    model.toDictionary()

您可以为任何字段的值更改设置选择器触发器。

   // Setup selector trigger
   model.property("Name", target: self, selector: #selector(ChangeName), on: .onChange)

   // Change value then ChangeName will be called
   model.name = "New name"

限制

  • 无法使用参数设置选择器。
  • 字段名必须与 JSON 字段名匹配,我们应该提供字段描述来自定义映射。

属性

  • 支持 iOS 和 OSX。
  • 易于使用和定义。
  • 通过减少打字错误和无聊的手动映射代码来减少错误。
  • 通过处理从服务器接收的 JSON 时对 null、nil、NSNull 和可选包装的封装来减少错误。
  • 包含网络和许多实用工具。
  • 在工作原理和 Swift 中运行良好。
  • 具有绑定,当您在发布对象之前忘记移除 KVO 时,它将不会崩溃。
  • Mapper 使用运行时函数,但由于它在初始化 Model 首次时缓存所有属性,因此速度非常快。

缺点

  • 仍然是 Objective-C。
  • 仍然存在一些限制。

改进

  • 支持带有参数的选择器。