iOS Swift 将 Alamofire 响应存储到 Realm

Posted

技术标签:

【中文标题】iOS Swift 将 Alamofire 响应存储到 Realm【英文标题】:iOS Swift Storing Alamofire Response To Realm 【发布时间】:2017-03-30 10:22:54 【问题描述】:

我需要将 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)
                                
                        

【问题讨论】:

那么....,有什么问题? 我在 realm.write 中遇到错误“无法将 NSDictionary 类型的值转换为 NSArray” 1.使用try 更好地发现您的错误。 2. 在此处发布您的日志 【参考方案1】:

我建议使用这个:

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

【讨论】:

无法将“__NSDictionaryI”(0x10b646238)类型的值转换为“NSArray”(0x10b645dd8)。

以上是关于iOS Swift 将 Alamofire 响应存储到 Realm的主要内容,如果未能解决你的问题,请参考以下文章

iOS Swift 2 - 失败时的 Alamofire 打印响应值

iOS Swift 解析来自 Alamofire 的 JSON 响应

无法解析响应 alamofire swift 3

使用 Swift 3 和 Alamofire 4 后没有得到响应

尝试从响应 Swift Alamofire 将数据保存到核心数据时崩溃

Alamofire 自定义响应从 Alamofire v1.3 迁移到 3.0(和 Swift 2 语法)