gpt4 book ai didi

iOS Swift 3 将值存储到 Realm

转载 作者:行者123 更新时间:2023-11-28 19:31:24 26 4
gpt4 key购买 nike

在我的应用程序中,我使用 Realm 来存储和检索数据。这是我的 API 结果。我需要将这些值存储到 Realm 数据库中。

{
"cc": [
{
"id": 142,
"firm": 2,
"specialization_id": 2,
"specialization_name": "Alternative",
"number": "234",
"title": "Push test",
"ear": "2012",
"description": "",
"status": "Open",
"open_date": "2017-05-22",
"close_date": null,
"closing_comments": null,
"type_id": 99,
"type_name": "Tax Reference",
"record_id": 57,
"record_name": "Court of India",
"type": "Court",
"other_party": [],
"billing_type": "Retainer",
"applicants": [
{
"type": "Contact",
"id": 31816,
"name": "1.Mrs V.Dhanalakshmi & Another",
"email": null,
"firm_id": 2,
"company_id": null,
"has_portal_access": false,
"mobile_number": null,
"street_address": null,
"locality": null,
"city": null,
"state": null,
"postal_code": null,
"landmark": null
}
],
}
]
}

问题是我可以存储父值,例如 id、firm、specialisation_id 等。 但我无法存储值,例如 applicants 详细信息。这是我尝试过的代码:

import UIKit
import RealmSwift
import Realm
import ObjectMapper

class CaseResponseData: Mappable
{
var data:[CaseAreaEntity]?

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

func mapping(map: Map)
{
data <- map["court_case"]
}
}

//Case Details
class CaseAreaEntity:Object, Mappable
{
dynamic var id = 0
dynamic var firm_id = 0
dynamic var specialization_id = 0
dynamic var specialization_name = ""
dynamic var number = ""
dynamic var title = ""
dynamic var case_year = ""
dynamic var description2 = ""
dynamic var status = ""
dynamic var open_date = ""
dynamic var close_date = ""
dynamic var closing_comments = ""
dynamic var case_type_id = 0
dynamic var case_type_name = ""
dynamic var court_record_id = 0
dynamic var court_record_name = ""
dynamic var court_type = ""
dynamic var client_type = ""
dynamic var billing_type = ""
dynamic var referred_by = ""
var applicants = List<ApplicantsEntity>()


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

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


func mapping(map: Map)
{
id <- map["id"]
firm_id <- map["firm_id"]
specialization_id <- map["specialization_id"]
specialization_name <- map["specialization_name"]
number <- map["number"]
title <- map["title"]
case_year <- map["case_year"]
description2 <- map["description"]
status <- map["status"]
open_date <- map["open_date"]
close_date <- map["close_date"]
closing_comments <- map["closing_comments"]
case_type_id <- map["case_type_id"]
case_type_name <- map["case_type_name"]
court_record_id <- map["court_record_id"]
court_record_name <- map["court_record_name"]
court_type <- map["court_type"]
client_type <- map["client_type"]
billing_type <- map["billing_type"]
referred_by <- map["referred_by"]
applicants <- map["applicants"]
}
}
//Applicants Details
class ApplicantsEntity: Object,Mappable
{
dynamic var applicant_id = 0
dynamic var applicantable_type = ""
dynamic var id = 0
dynamic var name = ""
dynamic var first_name = ""
dynamic var last_name = ""
dynamic var email = ""
dynamic var firm_id = 0
dynamic var company_id = 0
dynamic var has_portal_access = Bool()
dynamic var mobile_number = ""
dynamic var phone_number = ""
dynamic var fax = ""
dynamic var website = ""
dynamic var street_address = ""
dynamic var locality = ""
dynamic var city = ""
dynamic var state = ""
dynamic var postal_code = ""
dynamic var landmark = ""

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

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

func mapping(map: Map)
{
applicant_id <- map["applicant_id"]
applicantable_type <- map["applicantable_type"]
id <- map["id"]
name <- map["name"]
first_name <- map["first_name"]
last_name <- map["last_name"]
email <- map["email"]
firm_id <- map["firm_id"]
company_id <- map["company_id"]
has_portal_access <- map["has_portal_access"]
mobile_number <- map["mobile_number"]
phone_number <- map["phone_number"]
fax <- map["fax"]
website <- map["website"]
street_address <- map["street_address"]
locality <- map["locality"]
city <- map["city"]
state <- map["state"]
postal_code <- map["postal_code"]
landmark <- map["landmark"]
}
}

当检索值时 applicant 值没有出现,因为这些值没有被存储。这是我尝试创建 Realm 的代码:

let JSON = response.result.value
let realm:Realm = try! Realm()
let resData = Mapper<CaseResponseData>().map(JSONObject: JSON)
try! realm.write
{
realm.delete(realm.objects(CaseAreaEntity.self))
realm.add((resData?.data!)!, update: true)
//realm.create(CaseAreaEntity.self, value: (resData?.data!)!, update: true)
}

最佳答案

尝试改变这两个东西,改变你的 applicants 属性的类型:

let applicants = List<ApplicantsEntity>()

并将主键添加到两个类:

class ApplicantsEntity: Object
{
dynamic var "id" = ""
...
override static func primaryKey() -> String? {
return "id"
}
}

有了这个,你应该能够使用像魅力一样工作的这条线:

realm.create(CaseAreaEntity.self, value: json, update: true)

查看 Realm 文档的 [这部分][1] 了解更多详细信息。

编辑:您不需要 ObjectMapper。尝试像这样修改您的 Object 子类:

import RealmSwift

class CaseAreaEntity: Object {
dynamic var id = 0
dynamic var firm_id = 0
dynamic var specialization_id = 0
dynamic var specialization_name = ""
dynamic var number = ""
dynamic var title = ""
dynamic var case_year = ""
dynamic var description2 = ""
dynamic var status = ""
dynamic var open_date = ""
dynamic var close_date = ""
dynamic var closing_comments = ""
dynamic var case_type_id = 0
dynamic var case_type_name = ""
dynamic var court_record_id = 0
dynamic var court_record_name = ""
dynamic var court_type = ""
dynamic var client_type = ""
dynamic var billing_type = ""
dynamic var referred_by = ""
var applicants = List<ApplicantsEntity>()

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

class ApplicantsEntity: Object {
dynamic var applicant_id = 0
dynamic var applicantable_type = ""
dynamic var id = 0
dynamic var name = ""
dynamic var first_name = ""
dynamic var last_name = ""
dynamic var email = ""
dynamic var firm_id = 0
dynamic var company_id = 0
dynamic var has_portal_access = Bool()
dynamic var mobile_number = ""
dynamic var phone_number = ""
dynamic var fax = ""
dynamic var website = ""
dynamic var street_address = ""
dynamic var locality = ""
dynamic var city = ""
dynamic var state = ""
dynamic var postal_code = ""
dynamic var landmark = ""

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

然后您可以添加一个扩展以直接从 try JSONSerialization.jsonObject(with:, options:) 的输出创建对象:

import RealmSwift

extension CaseAreaEntity {
class func create(from jsonResponse: [String: Any]) throws {
do {
let realm = try Realm()
if let jsonCc = jsonResponse["cc"] as? [[String: Any]] {
try realm.write {
for json in jsonCc {
realm.create(CaseAreaEntity.self, value: json, update: true)
}
}
}
} catch { throw error }
}
}

有了上面的类和扩展,你可以简单地做到这一点:

if let jsonResult = try JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers) as? [String: Any] { 
try CaseAreaEntity.create(from: jsonResult)
}

jsonResult 是您在问题的第一个代码段中显示的 json 输出。

  [1]: https://realm.io/docs/swift/latest/#json

关于iOS Swift 3 将值存储到 Realm ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44113886/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com