通过键RestKit iOS迭代

时间:2015-03-13 19:59:08

标签: ios swift mapping restkit

如何使用RestKit映射?我想尽可能地概括这一点,因为每个钥匙代表停车场的名称,如果添加了停车场,我不需要更改代码。

{
    Arco Do Cego: 
        {
            address: "Parque do Arco Cego, Avenida João Crisóstomo, 1000-178 Lisboa",
            campus: "Alameda",
            description: "Parque Estacionamento do Arco do Cego",
            freeSlots: 68,
            latlng: "38.73640895,-9.14313902",
            name: "Arco do Cego",
            total: 70,
            workingHours: "24h"
        }
}

我想要这样的事情:

import Foundation
import CoreData

@objc(ParkingSpots)
class ParkingSpots: NSManagedObject {

    @NSManaged var id: String
    @NSManaged var parks: NSSet

}

@objc(Park)
class Park: NSManagedObject {

    @NSManaged var address: String
    @NSManaged var campus: String
    @NSManaged var desc: String
    @NSManaged var freeSlots: NSNumber
    @NSManaged var latlng: String
    @NSManaged var name: String
    @NSManaged var total: NSNumber
    @NSManaged var workingHours: String
    @NSManaged var parkingSpot: ParkingSpots
}

在Java中我会:

for (Map.Entry<String, JsonElement> entry : jObj.entrySet()) {
    String name = entry.getKey();
    Parking parking = gson.fromJson(entry.getValue(), Parking.class);
    parking.setName(name);
}

谢谢。我正在使用iOS8和swift

1 个答案:

答案 0 :(得分:0)

您需要使用addAttributeMappingFromKeyOfRepresentationToAttribute:,它允许您提取该密钥并将其用作值。所以在这种情况下你不会使用字典中的名字,你可以使用键来映射你的名字。