iOS Swift 存储对领域的 Alamofire 响应

iOS Swift Storing Alamofire Response To Realm

我需要将 alamofire JSON 响应直接存储到领域存储。这是我从 alamofire JSON.

得到的回复
{
  "all": [
    {
      "date": "2017-03-30T00:00:00.000Z",
      "subject": "loe",
      "desc": "",
      "id": 13,
      "number": "19/312/2012",
      "title": "loe",
      "name": "Supreme",
      "type": "Event",
      "practice": 20,
      "contact": "",
      "object": "{\"id\":20,\"id\":13,\"name\":\"loe\",\"time\":\"2017-03-30T00:00:00.000Z\",\"end\":\"2017-03-31T00:00:00.000Z\",\"creator\":\"user\",\"created_by\":132,\"des\":\"\",\"created_at\":\"2017-03-30T08:22:31.150Z\",\"updated_at\":\"2017-03-30T08:23:04.944Z\",\"judge\":null,\"purpose\":null,\"google_event_id\":null,\"is_completed\":false,\"business\":null,\"last_notified\":\"2017-03-30T08:23:04.926Z\",\"next\":null,\"business_date\":null,\"business\":false}"
    },
    {
      "date": "2017-03-30T00:30:00.000Z",
      "subject": "user",
      "desc": "Loe",
      "id": 138,
      "number": "19/312/2012",
      "title": "loe user",
      "name": "Supreme India",
      "type": "Appointment",
      "practice": 6,
      "contact": 91,
      "object": "{\"id\":20,\"id\":13,\"name\":\"loe\",\"time\":\"2017-03-30T00:00:00.000Z\",\"end\":\"2017-03-31T00:00:00.000Z\",\"creator\":\"user\",\"created_by\":132,\"des\":\"\",\"created_at\":\"2017-03-30T08:22:31.150Z\",\"updated_at\":\"2017-03-30T08:23:04.944Z\",\"judge\":null,\"purpose\":null,\"google_event_id\":null,\"is_completed\":false,\"business\":null,\"last_notified\":\"2017-03-30T08:23:04.926Z\",\"next\":null,\"business_date\":null,\"business\":false}"
    }

这是我用于领域存储的代码

class PracticeArea:Object,Mappable
{
    dynamic var contact = 0
    dynamic var id = ""
    dynamic var number = ""
    dynamic var title = ""
    dynamic var name = ""
    dynamic var date = ""
    dynamic var description2 = ""
    dynamic var object = ""
    dynamic var practice = 0
    dynamic var subject = ""
    dynamic var type = ""

    override static func primaryKey() -> String?
    {
        return "contact"
    }

    //Impl. of Mappable protocol
    required convenience init?(map: Map)
    {
        self.init()
    }

    func mapping(map: Map)
    {
        contact    <- map["contact"]
        id <- map["id"]
        number <- map["number"]
        title <- map["title"]
        name <- map["name"]
        date <- map["date"]
        description2 <- map["description"]
        object <- map["object"]
        practice <- map["practice"]
        subject <- map["subject"]
        type <- map["type"]
    }
}

用于存储值的代码:

let PracticeTestValues = response.result.value!
let realm:Realm = try! Realm()
                        try! realm.write
                        {
                                for all in PracticeTestValues as! [Any]
                                {
                                    realm.add(all as! Object, update: true)
                                }
                        }

我建议使用这个:

for all in PracticeTestValues as? NSArray
 {
    realm.add(all as! Object, update: true)
  }