转换一个字典数组并存储到Swift中的Realm

时间:2016-11-19 14:30:33

标签: ios swift dictionary mapping realm

我有一个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之前从数组中获取字典。

0 个答案:

没有答案