1

I have an issue with using a NSPredicate to filter a DaDataTable using a NSDate, when I apply the NSPredicate to the DaDataTable rows and I store the result in a NSMutableArray it always contains zero rows.

Let’s say I have the following table

Field1
2013-04-10 05:00:00 +0000
2013-04-10 05:00:00 +0000
2013-04-11 05:00:00 +0000
2013-04-11 05:00:00 +0000
2013-04-12 05:00:00 +0000

And the NSPredicate is…

NSPredicate *predicate = [@"" stringByAppendingFormat:@"(%@ == ‘%@‘)”, filter.realField, filter.value];

where filter.realField is Field1 and filter.value is the NSDate I want to use, let’s say it’s 2013-04-10 05:00:00 +0000

In theory, it should return a NSMutableArray with two rows. But as I said early, it always returns an array with zero rows…

Here is the code that uses the predicate to filter the DADataTable (dataTable) which contains the table I mentioned early…

    if (predicate) {
        NSMutableArray *array = [NSMutableArray arrayWithArray:[dataTable rowsFilteredUsingPredicate:predicate]];
        if (array.count > 0) {
            [dataTable setRows:array];
            return YES;
        }
        else {
            //Alert that says the array is empty…
            return NO;
        }
    }
    else {
        return NO;
    }

And if I try to set a range between two NSDate, one that contains the day I choose to use in the predicate (2013-04-10 05:00:00 +0000) and one with the next day of that date (2013-04-11 05:00:00 +0000). To obtain the next day i used the code of Zaky German in How do i add 1 day to a NSDate?

but in this case I changed the NSPredicate like this

NSPredicate *predicate = [@"" stringByAppendingFormat:@"(%@ >= ‘%@‘) AND (%@ <= ‘%@‘)”, filter.realField, filter.value, filter.realField, nextDay];

it gives me the following error.

-[__NSCFString timeIntervalSinceReferenceDate]: unrecognized selector sent to instance 0x7fd50940
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString timeIntervalSinceReferenceDate]: unrecognized selector sent to instance 0x7fd50940'

*** First throw call stack:
(
0   CoreFoundation                      0x04aed946 __exceptionPreprocess + 182
1   libobjc.A.dylib                     0x04776a97 objc_exception_throw + 44
2   CoreFoundation                      0x04af55c5 -[NSObject(NSObject) doesNotRecognizeSelector:] + 277
3   CoreFoundation                      0x04a3e3e7 ___forwarding___ + 1047
4   CoreFoundation                      0x04a3dfae _CF_forwarding_prep_0 + 14
5   CoreFoundation                      0x04a1b714 -[NSDate compare:] + 68
6   Foundation                          0x041a8d12 -[NSComparisonPredicateOperator performPrimitiveOperationUsingObject:andObject:] + 413
7   Foundation                          0x0416e913 -[NSPredicateOperator performOperationUsingObject:andObject:] + 308
8   Foundation                          0x0416deb5 -[NSComparisonPredicate evaluateWithObject:substitutionVariables:] + 345
9   Foundation                          0x0419eb26 -[NSCompoundPredicateOperator evaluatePredicates:withObject:substitutionVariables:] + 254
10  Foundation                          0x0419e99c -[NSCompoundPredicate evaluateWithObject:substitutionVariables:] + 292
11  Foundation                          0x0416dd54 -[NSPredicate evaluateWithObject:] + 48
12  Foundation                          0x0416dcd3 _filterObjectsUsingPredicate + 437
13  Foundation                          0x0416da7b -[NSArray(NSPredicateSupport) filteredArrayUsingPredicate:] + 328
14  iSIREGob                            0x00d47174 -[DADataTable rowsFilteredUsingPredicate:] + 68
15  iSIREGob                            0x000ed291 -[DataSource doSearch:campoAFiltrar:VasABuscar:] + 1537
16  iSIREGob                            0x000cc12b -[SireGenListGetBrowse searchBarSearchButtonClicked:] + 587
17  UIKit                               0x0329d159 -[UISearchBar(UISearchBarStatic) _searchFieldReturnPressed] + 84
18  libobjc.A.dylib                     0x0478c7cd -[NSObject performSelector:withObject:withObject:] + 84
19  UIKit                               0x02f3b23d -[UIApplication sendAction:to:from:forEvent:] + 99
20  UIKit                               0x02f3b1cf -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 64
21  UIKit                               0x0306ee86 -[UIControl sendAction:to:forEvent:] + 69
22  UIKit                               0x0306f2a3 -[UIControl _sendActionsForEvents:withEvent:] + 598
23  UIKit                               0x03079a37 -[UIFieldEditor insertText:] + 275
24  UIKit                               0x0378f46a -[UITextField insertText:] + 60
25  UIKit                               0x031802ab -[UIKeyboardImpl insertText:] + 107
26  UIKit                               0x0317c61d -[UIKeyboardImpl performKeyboardOutput:] + 551
27  UIKit                               0x0317c27a __55-[UIKeyboardImpl handleKeyboardInput:executionContext:]_block_invoke_2 + 157
28  UIKit                               0x037b40a2 -[UIKeyboardTaskQueue continueExecutionOnMainThread] + 404
29  libobjc.A.dylib                     0x0478c771 -[NSObject performSelector:withObject:] + 70
30  Foundation                          0x04164f20 __NSThreadPerformPerform + 330
31  CoreFoundation                      0x04a111df __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
32  CoreFoundation                      0x04a06ced __CFRunLoopDoSources0 + 253
33  CoreFoundation                      0x04a06248 __CFRunLoopRun + 952
34  CoreFoundation                      0x04a05bcb CFRunLoopRunSpecific + 443
35  CoreFoundation                      0x04a059fb CFRunLoopRunInMode + 123
36  GraphicsServices                    0x056e024f GSEventRunModal + 192
37  GraphicsServices                    0x056e008c GSEventRun + 104
38  UIKit                               0x02f398b6 UIApplicationMain + 1526
39  iSIREGob                            0x00037c8d main + 141
40  libdyld.dylib                       0x05255ac9 start + 1
41  ???                                 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

I hope you can help me to solve this, and Thanks in advance!

Community
  • 1
  • 1
xzrudy
  • 93
  • 1
  • 8

1 Answers1

3

You are not creating your predicate properly. You are creating an NSString object that you are then typecasting as a predicate and passing that through to the array. This explains the error you're getting. The NSString object can't respond to the selectors of NSPredicate and this raises an exception.

Create a predicate like this:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(%K >= %@) AND (%K < %@)", keyPath, value, otherKeyPath, otherValue];

Notice how I use %K to represent key paths an only %@ to represent values. This is important. You should read the predicate programming guide before just jumping right in. Also it is important to understand the objective-c syntax where @"" is an NSString object.

Rob Sanders
  • 5,197
  • 3
  • 31
  • 58
  • Thanks you!! it worked. I guess because in the past I used `%@` for the same purpose instead of `%K`and it worked, i kept using it the same way... – xzrudy Dec 15 '14 at 19:59