gpt4 book ai didi

swift - 如何在 Realm Swift 中比较两个值

转载 作者:行者123 更新时间:2023-11-30 12:44:29 25 4
gpt4 key购买 nike

我有一个名为 NewsCountRealm 数据库。仅当有新新闻时(分别当 newsCount 更改时),我才需要下载新新闻。我通过数据解析进行了比较。但我无法正确比较它们。你如何比较它们?

myImage

这是我的代码

private func parseJSONData(_ data: Data) {
do {
let temp: NSString = NSString(data: data, encoding: String.Encoding.utf8.rawValue)!
let myNSData = temp.data(using: String.Encoding.utf8.rawValue)!

guard let jsonResult = try JSONSerialization.jsonObject(with: myNSData, options: JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary else {
return
}
guard let jsonNews = jsonResult["categories"] as? [AnyObject] else {
print("Empty array")
return
}

let realm = try Realm()
let category = realm.objects(NewsCount.self)
var array = [Int]()

for i in category {
array.append(i.newsCount)
}

print(array)
print("News COUNT2 \(category)")

for jsonnewes in jsonNews {
let newsJson = NewsCount()

//HERE I COMPARE
if !UserDefaults.standard.bool(forKey: "AppStarted") || jsonnewes["count"] as! Int > array[jsonnewes as! Int]{
newsJson.newsID = jsonnewes["term_id"] as! Int
newsJson.newsCount = jsonnewes["count"] as! Int
//print("News COUNT2 \(newsJson.newsCount)")
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "downloadNew"), object: nil)
} else {
newsJson.newsID = jsonnewes["term_id"] as! Int
newsJson.newsCount = jsonnewes["count"] as! Int
//print("News COUNT3 \(newsJson.newsCount)")
}

insertOrUpdate(newsJson)
}
} catch {
print(error)
}
}

最佳答案

因为 Realm 使用 RealmOptional 来使用 Int 值,所以您必须调用 RealmOptional 的值属性

尝试更改此:

for i in category {
array.append(i.newsCount.value)
}

关于swift - 如何在 Realm Swift 中比较两个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41805047/

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