【问题标题】:iOS Swift Storing Alamofire Response To RealmiOS Swift 将 Alamofire 响应存储到 Realm
【发布时间】:2023-04-06 07:13:01
【问题描述】:

我需要将 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. 在此处发布您的日志

标签: ios swift3 realm realm-migration realm-list


【解决方案1】:

我建议使用这个:

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

【讨论】:

  • 无法将“__NSDictionaryI”(0x10b646238)类型的值转换为“NSArray”(0x10b645dd8)。
猜你喜欢
  • 2016-02-17
  • 2019-04-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-02
  • 1970-01-01
  • 2017-11-13
  • 1970-01-01
相关资源
最近更新 更多