2 Replies
      Latest reply: Sep 21, 2016 8:23 PM by dsbirch RSS
      dsbirch Level 1 Level 1 (0 points)

        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?

        • Re: Error filtering NSArray with predicate
          eskimo Apple Staff Apple Staff (6,975 points)

          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"

            • Re: Error filtering NSArray with predicate
              dsbirch Level 1 Level 1 (0 points)

              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.