NSCompoundPredicate正在给出意想不到的结果

时间:2016-10-04 20:47:44

标签: swift

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

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

1 个答案:

答案 0 :(得分:0)

您是将这些属性存储为字符串而不是整数? 可能是你正在对字符串值进行比较,而不是它们的真实数值,这会产生这些意想不到的结果。