ios - Swift Alamofire 映射器

标签 ios swift

Swift Alamofire Mapper 不会将字符串数组映射为 null。

例如,我们有:

["first_item", "second_item", null]

当我使用 Alamofire Mapper 时,它返回空数组,如 -> [], 当我们的服务响应如下时,如何处理数组中的空值?

谢谢!

最佳答案

最后我找到了如何计算这个:)我的模型如下:

    import ObjectMapper
    struct DataItems: Mappable {

    var rulesUrl: String? 
    var isAvailable: Bool? 
    var companyName: String? 
    var offers: [String]? 

    required init?(map: Map){}

    func mapping(){
    ..... mapping
    }
}

在这个模型中,当我想将我的responseString映射到这个模型时,如果在优惠中我们有来自服务器的类似[“offer_1”,“offer_2”,null]的东西,alamofire返回nil优惠。但之后我改变了报价:[String]?提供[字符串?]?然后它就如我想要的那样工作了:)现在它就像下面这样:

import ObjectMapper
struct DataItems: Mappable {

  var rulesUrl: String? 
  var isAvailable: Bool? 
  var companyName: String? 
  var offers: [String?]? 

  required init?(map: Map){}

  func mapping(){
..... mapping
   }

}

我希望它可以帮助其他人。

关于ios - Swift Alamofire 映射器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44745898/

相关文章:

ios - Swift:递归值类型

ios - 如何在 iOS 中从一个下拉菜单导航到下一个下拉菜单?

iOS swift imageView无法在TableViewCell中隐藏

swift - 另一个 View /按钮 swift 3 顶部的手势识别器

ios - 在 Swift 中强制自定义 AVSpeech 和 AVSpeechUtterance 中单词的发音

SwiftUI:从屏幕上的 CGPoint 转换为 View 中的 CGPoint

ios - UITableViewCell 内标签的 NSLayoutConstraint

c++ - 在 Xcode iOS 项目中使用 C++ 静态库 (CCFis)

javascript - iOS 是否提供 AirPlay 相关的 HTML5 视频事件?

xcode - 如何获取变量保存在 SpriteKit swift Xcode 的所有场景中