NSPredicate - unable to filter on date range

I’m creating an NSPredicate with a date range filter, and using NSArray’s filteredArrayUsingPredicate to return the filtered array.

The predicate runs ok, but it fails on filtering with an ‘unrecognized selector sent to instance’ error.

Here’s the code:

    set theYear to "1985"
	set df to current application's NSDateFormatter's new()
	(df's setDateFormat:"yyyy-MM-dd")
	
	set yearStartString to theYear & "-01-01"
	set yearEndString to theYear & "-12-31"
	set yearStart to (df's dateFromString:yearStartString) as date
	set yearEnd to (df's dateFromString:yearEndString) as date
	
    -- init predicate
	set theDateRangePredicate to current application's NSPredicate's predicateWithFormat_("docDate > %@ AND docDate < %@", yearStart, yearEnd)
	
	-- Filter the array using the predicate
	set filteredArray to (current application's NSArray's arrayWithArray:docData)'s filteredArrayUsingPredicate:theDateRangePredicate

‘docData’ is a list of dictionaries with ‘docDate’ being a date element in the dict.

Here I’ve used an Applescript date for predicate parameters. But, I’ve also tried explicitly converting to NSDate. Both methods failed.

I cannot reproduce the error. It works as expected, both coercions to date are not necessary.

My sample array is

set docData to {{docDate:date "Tuesday, 1 January 1985 at 00:00:00"}, {docDate:date "Monday, 1 July 1985 at 00:00:00"}}

Be aware that the first item is ignored because NSDateFormatter considers the current time zone and the first date is 31 December 1984 at 23:00:00 in UTC

Thanks for the effort; I’m stumped.

Thought it might be the docDate field in the list, but it looks good in Script Debugger:

image

Gonna use an AS repeat loop for now until I can figure this out.

Hi.

Probably not much help, but the only way I’ve been able to reproduce the error (so far) is if:

  1. Any of the items in docData is an empty list, or a list of lists, or a list of records, or the ASObjC equivalent of any of these, or if:
  2. The docDate value in any of the records or dictionaries isn’t an AS date or an NSDate.

No problem’s caused by any of the dictionaries not actually having a docDate property.