【问题标题】:Realm + Swift update object with JSON stringRealm + Swift 使用 JSON 字符串更新对象
【发布时间】:2016-11-05 15:55:55
【问题描述】:

我正在使用 alamofire 从服务器获取响应 JSON,然后使用 ObjectMapper 将字符串映射到 Realm 对象。

    The realm object is:
    class SolutionVideo: Object, Mappable {

dynamic var svID = 0
dynamic var solutionTitle = ""
dynamic var videoName = ""
dynamic var relatedInfo = ""
dynamic var shortDesc = ""

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

required convenience init?(_ map: Map) {
    self.init()
}

func mapping(map: Map) {
    svID <- map["svID"]
    solutionTitle <- map["solutionTitle"]
    videoName <- map["videoName"]
    relatedInfo <- map["relatedInfo"]
    shortDesc <- map["shortDescription"]
}

}

    The json string is:
    [
    {
        "svID": "10",
        "solutionTitle": "Video10",
        "videoName": "Video10",
        "realtedInfo": "",
        "shortDescription": ""
    },
    {
        "svID": "9",
        "solutionTitle": "Video9",
        "videoName": "Video9",
        "realtedInfo": "",
        "shortDescription": ""
    }
     ]



     in my viewController:
         @IBAction func updateBtn(sender: AnyObject) {
    // download file to update Realm
    let url = "http://janicedemo.com/updates.json"
    Alamofire.request(.GET, url).responseArray { (response: Response<[SolutionVideo], NSError>) in
        let Array = response.result.value
        print(Array)
        if let Array = Array {
            for video in Array {
                let dbURL = Realm.Configuration.defaultConfiguration.fileURL
                let realm = try! Realm(fileURL: dbURL!)
                try! realm.write{
                    print("will save")
                    realm.add(video, update: true)
                }
            }
        }
    }

问题是我可以成功添加对象。但是 svID(primmark key) 保持 0,而不是 10 或 9(在 JSON 中设置)。是因为我将默认值设置为 svID 吗?有人可以给我一个提示吗?谢谢

【问题讨论】:

  • 你已经在使用 AlamofireObjectMapper 扩展了吗?
  • 是的,我已经用过了 :)

标签: ios swift realm objectmapper


【解决方案1】:

试试

class SolutionVideo: Object, Mappable {

dynamic var svID = 0
dynamic var solutionTitle = ""
dynamic var videoName = ""
dynamic var relatedInfo = ""
dynamic var shortDesc = ""

func setCompoundID(id: Int) {
    self.svID = svID
    compoundKey = compoundKeyValue()
}

func setCompoundID(id: Int) {
    self.solutionTitle = solutionTitle
    compoundKey = compoundKeyValue()
}

func setCompoundID(id: Int) {
    self.videoName = videoName
    compoundKey = compoundKeyValue()
}

func setCompoundID(id: Int) {
    self.relatedInfo = relatedInfo
    compoundKey = compoundKeyValue()
}

func setCompoundID(id: Int) {
    self.shortDesc = shortDesc
    compoundKey = compoundKeyValue()
}


dynamic lazy var compoundKey: String = self.compoundKeyValue()

override static func primaryKey() -> String? {
return “compoundKey”
}

func compoundKeyValue() -> String {
    return "\(svID)\(solutionTitle)\(videoName)\(relatedInfo)\(shortDesc)”
}

}

【讨论】:

  • 我用的是AlamofireObjectMapper,它结合了alamofire和objectmapper。
  • 查看我的更新。那会奏效吗?使用自定义设置器,您可以确保始终更新 CompoundKey。 lazy var 确保您第一次访问该属性时,它将从您设置的内容中获取。
  • 不对。当我试图映射它时,它会抛出一个错误,即 svID 是 get-only
【解决方案2】:

我能想到的主要是主键值作为字符串而不是正确的整数(即"10" 而不是简单的10)。映射器可能不够聪明,无法处理字符串到整数的转换,所以它只是默认为 0。

According to the ObjectMapper documentation,您应该可以在映射函数中内联执行此转换:

svID <- (map["svID"], TransformOf<Int, String>(fromJSON: { Int($0!) }, toJSON: { $0.map { String($0) } }))

另外,作为旁注,我注意到您的一个 JSON 键名有拼写错误:"realtedInfo"。所以我建议仔细检查是否也有效。 :)

【讨论】:

    猜你喜欢
    • 2017-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-24
    • 1970-01-01
    相关资源
    最近更新 更多