我目前正在使用Swift的ObjectMapper (参见:https://github.com/Hearst-DD/ObjectMapper/)将String
从一个HTTP请求转换为一个自定义类的对象。我从请求中得到的JSON是一个JSON Array,我想将它从CustomObject
类型转换为Array
。
我试过这样做:
var object = Mapper<Array<CustomObject>>().map(string: json)
但是我得到了一个错误:Can not find member 'map'
。
这应该怎么做呢?
编辑:这是我的CustomObject
类,现在名为ProductVariant
public class ProductVariant: Mappable {
/* Attributes */
public var id = 0
// var size : Size = nil
public var SKU = ""
public var stock = 0
public var numberOfDefects = 0
/* Constructors */
public init?() {
// Empty Constructor
}
required public init?(_ map: Map) {
mapping(map)
}
/* Methods */
public func mapping(map: Map) {
id <- map["id"]
SKU <- map["SKU"]
stock <- map["stock"]
numberOfDefects <- map["numberOfDefects"]
}
}
发布于 2015-03-25 18:37:13
我找到了一个解决方案,看起来很管用:
var list: Array<ProductVariant> = Mapper<ProductVariant>().mapArray(string: json)
当我遍历数组时,它为我提供了CustomObject
的正确属性。
我的错误是,我试图将Array
放在Mapper
的类型中,如我的问题所示。
发布于 2017-02-28 19:48:37
另一种选择是
let products = Mapper<ProductVariant>().mapArray(JSONString: json)
发布于 2021-11-25 08:58:53
我认为您需要安装对象映射器pod并在文件中导入ObjectMapper
https://stackoverflow.com/questions/29238020
复制相似问题