我正在嘗試創建一個NSPredicate
和NSDate
s來查詢我的核心數據db。我在一個方法中形成一個名爲filterString
的NSMutableString
的謂詞。 queryCount
是一個NSInteger
,用於跟蹤追加了多少個查詢。在下面的塊,start
和end
是NSDate
小號NSPredicate with NSDate
if (start || end)
{
if (!queryCount)
{
[filterString appendString:@"("];
}
else
{
[filterString appendString:@" && ("];
}
// date range and exact date
if (start && end)
{
[filterString appendFormat:@"%@ >= %@ && %@ <= %@", kTransactionFilterDateKey, start, kTransactionFilterDateKey, end];
}
// only start
else if (start && !end)
{
[filterString appendFormat:@"%@ >= %@ && %@ <= %@", kTransactionFilterDateKey, start, kTransactionFilterDateKey, [NSDate distantFuture]];
}
// only end
else if (end && !start)
{
[filterString appendFormat:@"%@ >= %@ && %@ <= %@", kTransactionFilterDateKey, [NSDate distantPast], kTransactionFilterDateKey, end];
}
[filterString appendString:@")"];
queryCount++;
}
當我到形成實際謂詞對象(self.currentFilterString
是指其中上述塊是從方法)的塊:
我收到錯誤"Unable to parse the format string "(date >= 2014-09-24 04:00:00 +0000 && date <= 4001-01-01 00:00:00 +0000)""
這是標記爲//only start
的塊的結果。其他塊的結果相同。我試過使用不同的日期值而不是distantFuture
,但是這也不會改變任何東西。
我也試過了一個變化,就像"%@ BETWEEN %@", kTransactionFilterDateKey, @[start, end]
,但這給了我同樣的錯誤。
這是一個NSPredicate
使用日期的正確格式還是有不同的方法來做到這一點?我在網上找到的所有東西都讓我認爲我只是將NSDate
對象放入格式化的字符串中,就像我在做的一樣。
您可以嘗試的一件事是創建多個單獨的謂詞並設置複合謂詞:[NSCompoundPredicate andPredicateWithSubpredicates:@ [pred1,pred2]]; – mitrenegade 2014-09-24 21:33:14