排除HandyJSON中的属性

时间:2017-09-24 02:00:19

标签: ios json swift

我使用HandyJSOn框架来序列化和反序列化Swift3中的对象。现在我遇到了我想从这个过程中排除某些属性的问题。我试图按照GithHub页面上给出的步骤进行操作,但我无法开始工作:

'render' from react-snapshot was never called. Did you replace the call to ReactDOM.render()?

带有错误的编译器:

class MyClass : HandyJSON {
  private var excludeThisProperty : String

  public func mapping(mapper: HelpingMapper) {
    mapper >>> self.excludeThisProperty
  }
}

+++示例+++

binary operator >>> cannot be applied to operands of type HelpingMapper and String

1 个答案:

答案 0 :(得分:0)

请将您的字符串更改为可选:

private var excludeThisProperty : String?

示例代码:

let jsonString = "{\"excludeThisProperty\":\"sdfsdf\"}"

if let myclass = MyClass.deserialize(from: jsonString) {
    print(myclass)
}

class MyClass : HandyJSON {
    private var myPropertyDefault : String? = "example" // changed from let to var
    private var myProperty : String

    public required init() {
        myProperty = myPropertyDefault!
    }

    public func reset() {
        myProperty = myPropertyDefault!
    }

    public func mapping(mapper: HelpingMapper) {
        mapper >>> self.myPropertyDefault
    }
}