【问题标题】:Filter a DADataTable using a NSPredicate with a NSDate使用带有 NSDate 的 NSPredicate 过滤 DADataTable
【发布时间】:2014-12-15 17:27:24
【问题描述】:

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

假设我有下表

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。但正如我之前所说,它总是返回一个零行的数组......

这是使用谓词过滤DADataTable (dataTable) 的代码,其中包含我前面提到的表格……

    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)。为了获得第二天我在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

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

【问题讨论】:

    标签: objective-c nsdate nspredicate


    【解决方案1】:

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

    像这样创建一个谓词:

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

    请注意我如何使用 %K 来表示关键路径,而仅使用 %@ 来表示值。这个很重要。在直接进入之前,您应该阅读predicate programming guide。此外,了解objective-c 语法也很重要,其中@"" 是一个NSString 对象。

    【讨论】:

    • 谢谢!!有效。我猜是因为在过去我使用%@ 而不是%K 来实现相同的目的,并且它起作用了,我一直以同样的方式使用它......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-18
    • 2013-11-20
    • 1970-01-01
    • 2010-12-30
    • 2014-03-26
    相关资源
    最近更新 更多