使用带有NSDate的NSPredicate过滤DADataTable

时间:2014-12-15 17:27:25

标签: objective-c nsdate nspredicate

当我将NSPredicate应用于DaDataTable行时,我使用NSDate使用NSPredicate过滤DaDataTable时遇到问题将结果存储在NSMutableArray中,它总是包含零行。

假设我有下表

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

NSPredicate是......

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

其中filter.realFieldField1filter.value是我想要使用的NSDate,让我们说它是2013-04-10 05:00:00 +0000

理论上,它应返回一行NSMutableArray两行。但正如我早期所说,它总是返回一个零行的数组......

以下是使用谓词过滤DADataTabledataTable)的代码,其中包含我刚才提到的表格...

    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;
    }

如果我尝试设置两个NSDate之间的范围,其中一个包含我在谓词(2013-04-10 05:00:00 +0000)中选择使用的日期,以及包含该日期第二天的日期2013-04-11 05:00:00 +0000 1}})。为了获得第二天,我在How do i add 1 day to a NSDate?

中使用了Zaky German的代码

但在这种情况下我改变了NSPredicate这样的

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

它给了我以下错误。

-[__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

我希望你能帮助我解决这个问题,并提前致谢!

1 个答案:

答案 0 :(得分:3)

您没有正确创建谓词。您正在创建一个NSString对象,然后将其作为谓词进行类型转换并将其传递给数组。这解释了您获得的错误。 NSString对象无法响应NSPredicate的选择器,这会引发异常。

创建一个这样的谓词:

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

注意我如何使用%K来表示只有%@来表示值的关键路径。这个很重要。你应该在刚刚跳入之前阅读predicate programming guide。同样重要的是要理解客观-c语法,其中@&#34;&#34;是一个NSString对象。