Search code examples
iosswiftcore-datanspredicate

NSPredicate doesn't work for long long values


I have simple predicate like this to fetch all Code objects from Core Data:

let predicate = NSPredicate(format: "code.state != %@ AND code.validity != %d AND code.validity < %ld", "ACCEPTANCE", 0,  Date().timeIntervalSince1970 * 1000)

Now inside cellForRow I am printing what I have:

    print("++++++++++")
    print(product.code)
    print(product.code?.state)
    let a = product.code?.validity ?? 0
    print(a)
    print(Date().timeIntervalSince1970 * 1000)
    print(Double(a) < Date().timeIntervalSince1970 * 1000)

And then I have the following result:

code.state != "ACCEPTANCE" AND code.validity != 0 AND code.validity < 4789427390557216924
++++++++++
aaa
Optional("PACKED")
1614253260000
1612447824663.5361
false
++++++++++
product test 5
Optional("PACKED")
1616880420000
1612447824672.909
false
++++++++++
Produkt testowy 1
Optional("UNPACKED")
1611788400000
1612447824678.4438
true
++++++++++
qqq
Optional("UNPACKED")
1612306800000
1612447824702.927
true
++++++++++
test phone 2
Optional("UNPACKED")
1612393200000
1612447824718.4958
true

As you can see two of them are printed as false. So why they are fetched while they didn't?


Solution

  • The problem is that the %ld format in the predicate does not match the type of the argument (which is a Double). You have the following options:

    • %f with a Double,
    • %ld with an Int,
    • %@ with an NSNumber, that is the least error-prone method.

    Examples:

    NSPredicate(format: "code.validity < %f", Date().timeIntervalSince1970 * 1000)
    NSPredicate(format: "code.validity < %ld", Int(Date().timeIntervalSince1970 * 1000))
    NSPredicate(format: "code.validity < %@", NSNumber(value: Date().timeIntervalSince1970 * 1000))