gpt4 book ai didi

swift - NSCompoundPredicate 给出了意想不到的结果

转载 作者:行者123 更新时间:2023-11-30 13:01:31 26 4
gpt4 key购买 nike

我必须承认我对谓词还很陌生。我有一个用户填写的表单,并根据此信息,它应该搜索保存的数据并返回相关结果。其代码是:

let numberOfPlayers = numberOfPlayersPicked
let minTimeForPredicate = minTime
let maxTimeForPredicate = maxTime
let gameTime = "averageplaytime"
let minPlayers = "minplayers"
let maxPlayers = "maxplayers"
let minPlayersPredicate = NSPredicate(format: "%K <= %D", minPlayers, numberOfPlayers)
let maxPlayersPredicate = NSPredicate(format: "%K >= %D", maxPlayers, numberOfPlayers)
let minTimePredicate = NSPredicate(format: "%K >= %D", gameTime, minTimeForPredicate)
let maxTimePredicate = NSPredicate(format: "%K <= %D", gameTime, maxTimeForPredicate)
let andPredicate = NSCompoundPredicate.init(andPredicateWithSubpredicates: [minPlayersPredicate, maxPlayersPredicate, minTimePredicate, maxTimePredicate])

numberOfPlayersPicked、minTime 和 maxTime 来自上一屏幕。我觉得它返回错误信息的一个例子是当 numberOfPlayersPicked = 1、minTime = 120 和 maxTime = 999(时间由分段控件设置)时。检索到的数据有 minplayers = 1、maxplayers = 2 和 Averageplaytime = 60。我不确定为什么要检索此数据,显然我完全误解了某些内容!

编辑:我尝试打印大量结果,看看是否可以发现发生的任何情况。

print(andPredicate)
print(numberOfPlayers)
print(minTimeForPredicate)
print(maxTimeForPredicate)
print(games[0])

minplayers <= 1 AND maxplayers >= 1 AND averageplaytime >= 0 AND averageplaytime <= 30
1
0
30
<NSManagedObject: 0x6100000a5640> (entity: GameInCollection; id: 0xd0000000003c0000 <x-coredata://31C3474F-7C40-4C47-AF2D-883A76F922C6/GameInCollection/p15> ; data: {
averageplaytime = 150;
barcode = "";
gametitle = "1 5 150";
maxplayers = 5;
minplayers = 1;
})

print statements

print results

最佳答案

您是否将这些属性存储为字符串而不是整数?您可能正在对字符串值而不是其真实数值进行比较,这会产生此类意外结果。

关于swift - NSCompoundPredicate 给出了意想不到的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39861485/

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