ios - 排除 HandyJSON 中的属性

标签 ios json swift

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

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

+++ 示例+++

class MyClass : HandyJSON {
    private let myPropertyDefault : String? = "example"
    private var myProperty : String

    public required init() {
        myProperty = myPropertyDefault!
    }

    public func reset() {
        myProperty = myPropertyDefault!
    }

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

最佳答案

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

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
    }
}

关于ios - 排除 HandyJSON 中的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46385861/

相关文章:

ios - AFNetworking 设置没有扩展名的图片

java - JAX-RS、 jackson 和 JodaTime : not working automatic configuration

json - Node : saving JSON to MongoDB

swift - 寻找在 Swift 中从字符串中过滤文本的最佳方法

ios - 将数据从第一个 VC 传递到第三个 (Swift)

ios - XCTest 中 viewDidLoad 的设置值

jquery - 全屏覆盖打开时防止 body 在 iPhone 上滚动

ios - 使用 SDWebImage 和 FirebaseStorageUI 删除缓存 URL

java - 将 json 数据映射到 java bean

swift - 在 macOS SwiftUI TextEditor 中显示行号