0

I am currently facing issue while filter the array using predicate.

I have one modelclass named NoteModelClass

class NoteModelClass: NSObject {
    var Post_ID = Int()
    var NoteTitle = String()
    var NoteDescription = String()
    var FirstName = String()
    var LastName = String()
    var Address = String()
 }

I also called one init method to parse jsonObject into modelClass

init(dict:NSDictionary)
 {
        self.Post_ID = dict.value(forKey: "Post_ID") as! Int
        self.NoteTitle = dict.value(forKey: "NoteTitle") as! String
        self.NoteDescription = dict.value(forKey: 
        "NoteDescription") as! String
        self.FirstName = dict.value(forKey: "FirstName") as! String
        self.LastName = dict.value(forKey: "LastName") as! String
        self.Address = dict.value(forKey: "Address") as! String
}

I am parse json array like

   for item in mainDict (array of json)
   {
      let model = NoteModelClass(dict: item as! NSDictionary)
      self.arrProperty.add(model);
   }

Now when user search I am filtering my array based on search string like that

 let predicate = NSPredicate(format: "NoteTitle CONTAINS[c] %@", "test”)
 arrFilterUsingSearch = self.arrProperty.filtered(using: predicate) as! NSMutableArray;

But I am getting crash on this line

   arrFilterUsingSearch = self.arrProperty.filtered(using: predicate) as! NSMutableArray;

I have done same functionality in diffrentProject also but I dont know what I am doing wrong Also I am getting my model class in array like this :

Printing description of ((_TtC11NoteBuyerGo14NoteModelClass *)0x166738e0):

<NoteBuyerGo.NoteModelClass: 0x166738e0>

Can anyone give me solution or guide me that what I am doing wrong here ?

I am stuck with this , can anyone help me in this.

I am getting below crash:

*** Terminating app due to uncaught exception 'NSUnknownKeyException', 
reason: '[<NoteBuyerGo.NoteModelClass 0x14e5bdb0> 
valueForUndefinedKey:]: this class is not key value coding-compliant 
for the key NoteTitle.'

Help will be really appreciated.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Anita Nagori
  • 707
  • 1
  • 7
  • 21
  • 1
    Is there an error in console when it crashes ? Are you using Swift 3+ ? If yes, avoid use `NSStuff`, like `NSArray`, `NSDictionary`, prefers Swift versions. Also, don't use ";" in Swift. I'm wondering if the issue is because `filtered(using: predicate)` that might return a `NSArray` instead of a `NSMutableArray`. – Larme Sep 06 '18 at 10:00
  • Please give info like exactly what error displays on console, and value of NoteModelClass object. Most of the times, the exact error is clear from the message shown in console. – Skywalker Sep 06 '18 at 10:07
  • Yes i am using swift 4.1 , but the crash says *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ valueForUndefinedKey:]: this class is not key value coding-compliant for the key NoteTitle.' Where i am doing wrong ? any idea ? – Anita Nagori Sep 06 '18 at 10:08
  • Yes. The thing is, you are applying the predicate on the array and so, since the array does not have any value for key 'NoteTitle', it crashes. – Skywalker Sep 06 '18 at 10:10
  • array contains the NoteModelClass objects – Anita Nagori Sep 06 '18 at 10:10
  • Suggestions: Always give error logs if available. This saves time. Also, don't name your variable starting with an uppercase. If you use Swift classes (and not the old NSStuff), go with the `filter()` with and not a `NSPredicate`. Avoid using `value(forKey:)`, prefers `object(forKey:)` or simply the subscript one: `dict[key]`. And finally, avoid force unwrap (using the `!`) so many times. – Larme Sep 06 '18 at 10:18
  • Also: https://stackoverflow.com/questions/31323632/class-is-not-key-value-coding-compliant-for-the-key-statictexts/31323718#31323718 – Martin R Sep 06 '18 at 10:24
  • Since `NSPredicate` wants to use Objective-C access, you probably need to prefix all of your properties at `NoteModelClass` with `@objc`. – DarkDust Sep 06 '18 at 10:32
  • 2
    I have resolved this issue with MartinR ' solution link. I have added @objc keyword with all property of modelclass and issue resolved. – Anita Nagori Sep 06 '18 at 10:33
  • 1
    Thank you everyone for taking your precious time to look around in my issue. – Anita Nagori Sep 06 '18 at 10:35

0 Answers0