0

I have arrData of dictionary, I want to filter based on PERSON_ID, p_punchTime(string dd-MM-yyyy HH:mm:ss) using prevDate and currDate in NSPredicate

  "aarData": [
        {
          "PERSON_ID": 100000000530788,
          "p_comments": "",
          "p_person": " Amal Abutaleb",
          "p_personId": "100000000530788",
          "p_punchTime": "25-08-2020 10:32:53",
          "p_punchType": IN,
        },
        {
          "PERSON_ID": 100000000530788,
          "p_comments": "",
          "p_person": " Amal Abutaleb",
          "p_personId": "100000000530788",
          "p_punchTime": "25-08-2020 10:32:59",
          "p_punchType": OUT, 
        }
      ]

How to use Date Object/string (to dd-MM-yyyy HH:mm:ss) for using it in NSPredicate in Swift.

when I pass strCurrdate and strPrevdate in NSPredicate works but filter returns all aarData, so Does string Date in NSPredicate do not work in the filter?

My Code:

let formatter = DateFormatter()
formatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
let strCurrdate = formatter.string(from: (NHNetworkClock.shared()?.networkTime)! as Date )
let currdateObj = formatter.date(from: strCurrdate)! as NSDate    
print("currdateObj : \(currdateObj)")      //prints 2020-08-24 13:54:03 +0000 ,need dd-MM-yyyy HH:mm:ss
            
            
           
var dayComponent = DateComponents()
dayComponent.day = -1 // For removing one day (yesterday): -1
let theCalendar  = Calendar.current
let prevDate = theCalendar.date(byAdding: dayComponent, to:(NHNetworkClock.shared()?.networkTime)! as Date)
print("prevDate : \(prevDate)") 
let strPrevdate = formatter.string(from: prevDate!)  //gives string dd-MM-yyyy HH:mm:ss
let prevDateObj = formatter.date(from: strPrevdate)! as NSDate
print("prevDateObj : \(prevDateObj)")   //prints 2020-08-24 13:54:03 +0000, need dd-MM-yyyy HH:mm:ss
let resultPredicate = NSPredicate(format: "PERSON_ID CONTAINS %@ AND ( p_punchTime >= %@ AND p_punchTime <= %@)", NSString.init(format: "%@",Constants().EMPLOYEE_ID() as String),prevDateObj! as NSDate,currdateObj! as NSDate)
filteredArr = arrData.filtered(using: predicate) as NSArray 
//NOTE: aarData contains p_punchTime in format string dd-MM-yyyy HH:mm:ss

log

(lldb) po resultPredicate
PERSON_ID CONTAINS "100000000530788" AND p_punchTime >= CAST(619860816.000000, "NSDate") AND p_punchTime <= CAST(619947216.000000, "NSDate")

I'm getting this error

libc++abi.dylib: terminating with uncaught exception of type NSException

Any lead on this

Ace Allen
  • 11
  • 4
  • What type is p_punchtime? – Joakim Danielson Aug 25 '20 at 06:53
  • You have lots of force unwrapping there. Don't do that. Unwrap conditionally and hande `nil` – Paulw11 Aug 25 '20 at 08:46
  • Full error message would be useful. You should have a more explicit error, like that property(leypath) doesn't exists, etc. – Larme Aug 25 '20 at 09:35
  • "//NOTE: aarData contains p_punchTime in format string dd-MM-yyyy HH:mm:ss" + "//prints 2020-08-24 13:54:03 +0000, need dd-MM-yyyy HH:mm:ss" while `prevDateObj` is a `(NS)Date` object, that's a big MISCONCEPT of what `(NS)Date` is. Tell us what's `arrData` first, are they String or Date objects? Also, why not use directly `filter` method of Swift, and not use `NSArray`? In general, in Swift3+, prefers Stuff over NSStuff when possible. – Larme Aug 25 '20 at 09:37
  • arrData consists of string objects [{PERSON_ID, p_punchtime.. },..] i want to filter arrData based on previousDate and currentDate..i refered this [link](https://stackoverflow.com/questions/30520599/unable-to-query-for-date-using-nspredicate-in-swift/30520861#30520861) – Ace Allen Aug 25 '20 at 10:39
  • arrData is like [{PERSON_ID:153243, p_punchtime:2020-07-26 16:30:12,... },..] so p_punchtime is also string, now i know we cannot format NSDate object, so I used strCurrdate and strPrevdate in NSPredicate works but arrData.filter(using: predicate) as NSArray returns all aarData – Ace Allen Aug 25 '20 at 11:01
  • "arrData is like [{PERSON_ID:153243, p_punchtime:2020-07-26 16:30:12,... },..]": Please edit your question with that info, that's important info about why it's not working, why it's crashing (still missing the full error message). Also, put real code, because right now, I don't know if you are using a struct there, a tuple, it's in fact JSON, etc... – Larme Aug 25 '20 at 12:16
  • Ok. It's from JSON, so you have JSON, you need to parse the data first. Is that the case? Do you have a custom struct to represent it? – Larme Aug 25 '20 at 12:17

1 Answers1

0

I changed my NSPredicate to string Date, working now

let resultPredicate = NSPredicate(format: "PERSON_ID == %@ AND (p_punchTime >= %@ AND p_punchTime <= %@)", NSString.init(format: "%@",Constants().EMPLOYEE_ID() as String),strPrevdate,strCurrdate )
filteredArr = arrData.filtered(using: resultPredicate) as NSArray
Ace Allen
  • 11
  • 4