0

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?

1
  • @MartinR you are a genius as always;) Thank you, it works... Feb 4, 2021 at 14:47

1 Answer 1

1

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))
1
  • could you please check out my question ? I need your solution as vaccine :(
    – ayfer
    Mar 8, 2021 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.