gpt4 book ai didi

ios - Swift 从 CKRecord 返回添加整数

转载 作者:行者123 更新时间:2023-11-28 15:32:32 25 4
gpt4 key购买 nike

我有一个 CKRecord 返回 1 和 -1 的列表。我需要将所有这些加在一起以获得净值,但无法计算出来。

下面是一些代码:

let freezerQuery = CKQuery(recordType: "PumpingEntry", predicate: predicate)
let freezerSort = NSSortDescriptor.init(key: "FreezerQuantity", ascending: false)
freezerQuery.sortDescriptors = [freezerSort]

var freezerOperation = CKQueryOperation(query: freezerQuery)
freezerOperation.desiredKeys = ["FreezerQuantity"]
freezerOperation.recordFetchedBlock = { (record: CKRecord!) in
let freezerInteger: Int = record.object(forKey: "FreezerQuantity") as! Int

let freezerTotel = ?????????????

最佳答案

您需要在 block 外使用您的 freezerTotal 变量。然后只需将 freezerInteger 添加到 freezerTotal

var freezerTotal = 0
freezerOperation.recordFetchedBlock = { (record: CKRecord!) in
let freezerInteger: Int = record.object(forKey: "FreezerQuantity") as! Int
freezerTotal += freezerInteger
}

然后您可以在操作的完成 block 中使用总数:

freezerOperation.queryCompletionBlock = { (cursor, error) in
// do something with the total
}

关于ios - Swift 从 CKRecord 返回添加整数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44636412/

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