Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
193 views
in Technique[技术] by (71.8m points)

ios - NSPredicate doesnt 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 didnt?

question from:https://stackoverflow.com/questions/66047436/nspredicate-doesnt-work-for-long-long-values

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...