NSPredicate用于NSMetadataQuery匹配包含目标日期的kMDItemAccessedDates(array)? [英] NSPredicate(s) for NSMetadataQuery to match kMDItemAccessedDates (array) containing a target date?

查看:171
本文介绍了NSPredicate用于NSMetadataQuery匹配包含目标日期的kMDItemAccessedDates(array)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:。我发现了一个答案,但可能还是有更好的方法 - 请参阅下面的答案。

UPDATE: I found an answer though there may still be a better way - see the answer added below.

聚光灯导入器导入作为访问项目的日期数组的元数据。看起来像这样:

a spotlight importer imports metadata that is an array of dates in which an item was accessed. Looks like this:

kMDItemAccessedDates               = (
    "2012-06-18 07:00:00 +0000",
    "2013-01-10 08:00:00 +0000",
    "2013-01-15 08:00:00 +0000"
)

我想查询在用户输入的特定日期访问的任何这些文件,但我没有找到正确的语法来做到这一点。想知道是否可以做...

I'd like to query for any of these files that were accessed on a particular date the user enters but I'm not finding the right syntax to do this. Wondering if it's possible to do...

第一个问题是用户输入的日期代表一天,而不是日期和时间。

The first issue is that the user enters a date which represents a day, not a day and time.

似乎我需要搜索那天的任何日期,所以逻辑上: ADATE> = user_day_start&& ADATE <= user_day_end

Seemed like I need to search for any date that falls on that day, so logically: ADATE >= user_day_start && ADATE <= user_day_end

现在在元数据中有一个日期数组,所以我真的希望它匹配,如果这些日期的任何一个

Now there is an array of dates in the metadata so I really want it to match if ANY of those dates fall within the user_date day.

如果这样工作,效果会更好:

Be nice if this worked:

NSDate * date = [NSDate dateWithNaturalLanguageString: inWhenString];
NSDate * endDate = [NSDate dateWithTimeInterval: 86400 sinceDate: date];

predicate = [NSPredicate predicateWithFormat: 
              @"kMDItemAccessedDates between {%@, %@}", date, endDate];

但会引发异常:

Exception detected while handling key input.
2013-01-16 03:12:57.517 Chatty[96974:403] Unknown type of NSComparisonPredicate 
  given to NSMetadataQuery 
  (kMDItemAccessedDates BETWEEN {CAST(379540800.000000, "NSDate"),
  CAST(379627200.000000, "NSDate")}) 

似乎它可能只适用于单个日期而不是数组。将ANY添加到格式字符串中,如下所示:{%@,%@}之间的任何kMDItemAccessedDates ...没有帮助,仍然引发异常。

and seems like it might only work for a single date rather than an array. Adding the "ANY" to the format string like so: "ANY kMDItemAccessedDates between {%@, %@}" ... didn't help, still throwing an exception.

试过这个,它不会抛出异常,但是不是真的工作:

Tried this and it's not throwing an exception, but isn't really working either:

NSPredicate *p1 = [NSPredicate predicateWithFormat: @"kMDItemAccessedDates >= %@", date ];
NSPredicate *p2 = [NSPredicate predicateWithFormat: @"kMDItemAccessedDates <= %@", endDate ];
predicate = [NSCompoundPredicate andPredicateWithSubpredicates: [NSArray arrayWithObjects: p1, p2, nil]];

想知道如果有人有任何想法如何使这项工作...或知道这是不可能的像是要得到它们所有,然后做内存中的过滤器或东西;真的希望不是这样 - 它可能是一个长列表。)

Wondering if anyone has any ideas on how to make this work... or knows that it's impossible (like have to get them all and then do the filter in memory or something; really hope that's not the case - it could be a long list!).

推荐答案

因此,再花几个小时挖掘互联网搜索结果,尝试和排序即可。

So another few hours digging into internet search results and trying things and I sort of have this working.

NSDate * userDate = [NSDate dateWithNaturalLanguageString: inWhenString];
NSDateComponents * userDateComponents = [[NSCalendar currentCalendar] components: NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit fromDate: userDate];
NSDate * dayDate = [[NSCalendar currentCalendar] dateFromComponents: userDateComponents];
dNSLog(@"date %@", dayDate );
NSString * formatStr = [NSDateFormatter dateFormatFromTemplate: @"yyyy-MM-dd" options: 0 locale: nil];
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat: formatStr];

NSString * dayStr = [formatter stringFromDate: dayDate];
dayStr = [dayStr stringByAppendingString: @"*"];
dNSLog(@"dayString: %@", dayStr );
NSPredicate *p1 = [NSPredicate predicateWithFormat: @"%K >= CAST(%f, \"NSDate\")", @"kMDItemAccessedDates", [dayDate timeIntervalSinceReferenceDate] ];
NSPredicate *p2 = [NSPredicate predicateWithFormat: @"%K <= CAST(%f, \"NSDate\")", @"kMDItemAccessedDates", [dayDate timeIntervalSinceReferenceDate] + 86400.0f ];
predicate = [NSCompoundPredicate andPredicateWithSubpredicates: @[p1, p2]];

此整体方法可能还有其他问题(基于应用程序如何更新此日期数组)这可能需要我完全做其他事情,但这似乎回答了如何只抓取在某一天访问的项目的原始问题。

There may be other issues with this whole approach (based on how the app updates this array of dates) that may require me to do something else entirely, but this seems to answer the original question of how to fetch only items that were accessed on a particular day.

键是 CAST(%f,\NSDate \)我相信。

更好的方式,但我想我会把这里放在这里,以防其他人正在挖掘任何线索。

There might still be a better way, but I thought I'd put this up here in case someone else is digging around for any clues.

这篇关于NSPredicate用于NSMetadataQuery匹配包含目标日期的kMDItemAccessedDates(array)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆