Error filtering NSArray with predicate

I have a project I'm trying to convert to Swift 3.0, which includes the following code:


if let array = games?.array as? [SMCGame] {

let wonPredicate = NSPredicate(format: "actualTime <= %@ && (homeScore > opponentScore)", [date])

let wonArray = (array as NSArray).filtered(using: wonPredicate)

}


This works fine in Swift 2.2 and 2.3, but in 3.0 I get the following runtime error on the last line: -[_TtCs21_SwiftDeferredNSArray timeIntervalSinceReferenceDate]: unrecognized selector sent to instance 0x7f96614520d0


How can I get this working in Swift 3.0?

Accepted Reply

I think the problem is with your predicate construction.

let wonPredicate = NSPredicate(format: "actualTime <= %@ && (homeScore > opponentScore)", [date])

Shouldn’t that be:

let wonPredicate = NSPredicate(format: "actualTime <= %@ && (homeScore > opponentScore)", date as NSDate)

?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Replies

I think the problem is with your predicate construction.

let wonPredicate = NSPredicate(format: "actualTime <= %@ && (homeScore > opponentScore)", [date])

Shouldn’t that be:

let wonPredicate = NSPredicate(format: "actualTime <= %@ && (homeScore > opponentScore)", date as NSDate)

?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks for the help with this. Your suggestion was spot on.


I don't why it compiles in earlier versions of Swift with the date parameter as an array in the predicate initializer. That certainly didn't help to reduce the confusion when trying to convert to Swift 3.0. On the other hand, it would also be helpful if Predicate didn't translate "oneDate =< anotherDate" in predicate formats as referring to timeIntervalSinceReferenceDate, assuming that's what's going on under the covers.