我有一个Json类型的字典数组,但我只能存储第一个字典“rent”:
"prices": [
{
"type": "rent",
"currency": "MY",
"min": 1000000,
"max": 5000000
},
{
"type": "buy",
"currency": "MY",
"min": 4000000,
"max": 5000000
}
],
因此,在Listing.Swift中,我调用transform类将其转换为字典:
Listing.swift
_price <- (map["prices"], ListingPriceTransform())
ListingPriceTransform.swift
class ListingPriceTransform: TransformType {
typealias Object = ListingPrice
typealias JSON = [[String: AnyObject]]
func transformFromJSON(value: AnyObject?) -> Object? {
guard let jsonDictionariesArray = value as? [[String: AnyObject]] else { return nil }
for object in jsonDictionariesArray {
return ListingPrice.createModel(object)
}
return ListingPrice.createModel(Object)
}
func transformToJSON(value: Object?) -> JSON? {
return nil
}
}
ListingPrice.swift
func mapping(map: Map) {
type <- map["type"]
currency <- map["currency"]
minPrice <- (map["min"], IntTransform())
maxPrice <- (map["max"], IntTransform())
}
因此,我无法在ListingPriceTransform(for循环)中保存,以便在传递给ListingPrice.swift中的Map之前从数组中获取字典。