0

I'm trying to use the first block of code here, but it's written for an earlier version of swift. So I updated it for Swift 4. Here it is as in my Playground:

import UIKit

class Person: NSObject {
   let firstName: String
   let lastName: String
   let age: Int

   init(firstName: String, lastName: String, age: Int) {
      self.firstName = firstName
      self.lastName = lastName
      self.age = age
   }

   override var description: String {
     return "\(firstName) \(lastName)"
   }
}

let alice = Person(firstName: "Alice", lastName: "Smith", age: 24)
let bob = Person(firstName: "Bob", lastName: "Jones", age: 27)
let charlie = Person(firstName: "Charlie", lastName: "Smith", age: 33)
let quentin = Person(firstName: "Quentin", lastName: "Alberts", age: 31)
let people = [alice, bob, charlie, quentin]

let bobPredicate = NSPredicate(format: "firstName = %@", "Bob")
let smithPredicate = NSPredicate(format: "lastName = %@", "Smith")
let thirtiesPredicate = NSPredicate(format: "age >= 30")

(people as NSArray).filtered(using: bobPredicate)

On the last line (people as NSArray).filtered(using: bobPredicate)

I'm getting the error: error: Execution was interrupted, reason: signal SIGABRT. The process has been left at the point where it was interrupted, use "thread return -x" to return to the state before expression evaluation.

I'm not sure how to resolve this error. Thanks,

  • 5
    Don't needlessly extend `NSObject`. Don't use `NSArray`. Don't use `NSPredicate`. Use Swift arrays. Use the standard `filter` method of Swift collections. – rmaddy Dec 15 '17 at 15:21
  • 1
    Why are you going through that tutorial? Unless using a specific Obj-C based framework that requires you to use `NSPredicate`s (such as Realm), I'd recommend you switch to native Swift types. – Dávid Pásztor Dec 15 '17 at 15:22
  • The linked-to duplicate should answer your immediate question (NSPredicate is based on Key-Value coding, and in Swift 4 that requires members to be exposed explicitly to the Objective-C runtime). – Of course, as said above, not relying on the Objective-C runtime is the better alternative. – Martin R Dec 15 '17 at 15:46
  • Ultimately, I'm trying to built a NSPredicate for getting at my CoreData. So, yes I don't need to do any of this to work with the array, there was a purpose to it. To cycle through NSPredicate attempts faster than try something, start my app, watch it fail, repeat. So, maybe that is my mistake. I can't play with NSPredicate this way? – Cory Steers Dec 15 '17 at 15:53
  • I don't really understand the linked-to-duplicate but adding `@objc` above the `class Person: NSObject {` line didn't change anything. – Cory Steers Dec 15 '17 at 16:10
  • Is there any particular reason why you don't use the filter method of swit's array by passing a closure where you can decide the filtering conditions? – Giuseppe Lanza Dec 15 '17 at 16:20
  • Yes. See my comment above (sorry, I don't know how to reference a previous comment). – Cory Steers Dec 15 '17 at 16:24
  • @CorySteers: You have to mark the *property,* not the class. In your case `@objc let firstName: String`. – Martin R Dec 15 '17 at 17:43
  • @MartinR OK. Now I understand Both: ` @objcMembers class Person: NSObject { let firstName: String let lastName: String let age: Int ... ` and ` class Person: NSObject { @objc let firstName: String @objc let lastName: String @objc let age: Int ... ` worked. – Cory Steers Dec 15 '17 at 18:04
  • Oops. where did all my formatting go? :) – Cory Steers Dec 15 '17 at 18:06

0 Answers0