Both Spotlight and the NSPredicate
class implement a query string syntax, and though they are similar, they differ in several respects. One query string can be converted into the other string form, as long as the common subset of functionality is used.
- Spotlight和NSPredicate类都实现了查询字符串语法,尽管它们相似,但它们在几个方面有所不同。 只要使用公共的功能子集,就可以将一个查询字符串转换为另一个字符串形式。
Spotlight and NSPredicate
The NSMetadataQuery
class, which is the Cocoa interface to Spotlight, uses NSPredicate
in its API. Apart from this, there is no relationship between Spotlight and NSPredicate
. The Spotlight query string syntax is similar to, but different from, the NSPredicate
query string syntax. You can convert one query string into the other string form, as long as you use syntax that both APIs understand. Spotlight's query syntax is a subset of that of NSPredicate
. For a complete description of the Spotlight query expression syntax, see File Metadata Query Expression Syntax, and for a complete description of the NSPredicate
string syntax, see Predicate Format String Syntax.
- NSMetadataQuery类是Spotlight的Cocoa接口,在其API中使用NSPredicate。 除此之外,Spotlight和NSPredicate之间没有任何关系。 Spotlight查询字符串语法与NSPredicate查询字符串语法类似,但不同。 您可以将一个查询字符串转换为另一个字符串形式,只要您使用两个API都能理解的语法即可。 Spotlight的查询语法是NSPredicate的一个子集。 有关Spotlight查询表达式语法的完整说明,请参阅文件元数据查询表达式语法,有关NSPredicate字符串语法的完整说明,请参阅谓词格式字符串语法。
Spotlight requires comparison clauses to take the form "KEY operator VALUE"
and does not accept "VALUE operator KEY"
. Moreover, the kinds of attribute Spotlight accepts for VALUE
are more limited than those accepted by NSPredicate
. As a partial consequence of this limitation, you do not always have to quote literal strings in Spotlight queries. You can omit the quotes when VALUE
is a string and no special operators need to be applied to it. You cannot do this with an NSPredicate
query string, as the result would be ambiguous.
- Spotlight要求比较子句采用“KEY operator VALUE”形式,并且不接受“VALUE operator KEY”。 此外,Spotlight对VALUE接受的属性种类比NSPredicate接受的属性更受限制。 作为此限制的部分结果,您不必总是在Spotlight查询中引用文字字符串。 当VALUE是一个字符串并且不需要对其应用特殊运算符时,可以省略引号。 您不能使用NSPredicate查询字符串执行此操作,因为结果将不明确。
The syntax for denoting case- and diacritic-insensitivity for queries in Spotlight is different from the NSPredicate
version. In Spotlight, you append markers to the end of the comparison string (for example, "myAttribute == 'foo'cd"
). In NSPredicate
strings, you use the like
operator and prefix the markers within "[]"s (for example, "myAttribute like[cd] 'foo'"
). In both cases, 'cd'
means case-insensitive and diacritic-insensitive. Spotlight puts the modifiers on the value, NSPredicate
puts the modifiers on the operator.
- 在Spotlight中表示查询的case和diacritic-insensitivity的语法与NSPredicate版本不同。 在Spotlight中,您将标记附加到比较字符串的末尾(例如,“myAttribute =='foo'cd”)。 在NSPredicate字符串中,您使用like运算符并在“[]”中添加标记前缀(例如,“myAttribute like [cd]'foo'”)。 在这两种情况下,'cd'表示不区分大小写且对变音不敏感。 Spotlight将修饰符放在值上,NSPredicate将修饰符放在运算符上。
You cannot use an MDQuery
operator as the VALUE
of an NSPredicate
object "KEY operator VALUE"
string. For example, you write an “is-substring-of” expression in Spotlight like this: "myAttribute = '*foo*'"
; in NSPredicate
strings you use the contains
operator, like this: "myAttribute contains 'foo'"
. Spotlight takes glob-like expressions, NSPredicate
uses a different operator.
- 您不能将MDQuery运算符用作NSPredicate对象“KEY operator VALUE”字符串的VALUE。 例如,您在Spotlight中编写一个“is-substring-of”表达式,如下所示:“myAttribute ='* foo *'”; 在NSPredicate字符串中,您使用contains运算符,如下所示:“myAttribute contains'foo'”。 Spotlight采用类似于glob的表达式,NSPredicate使用不同的运算符。
If you use “*
” as left-hand-side key in a comparison expression, in Spotlight it means “any key in the item” and can only be used with ==
. You could only use this expression in an NSPredicate
object in conjunction with an NSMetadataQuery
object.
- 如果在比较表达式中使用“*”作为左侧键,则在Spotlight中它表示“项目中的任何键”,并且只能与==一起使用。 您只能在NSPredicate对象中与NSMetadataQuery对象一起使用此表达式。
Creating a Predicate Format String From a Spotlight Search in Finder
You can create a predicate format string from a search in Finder. Perform a search, save it, then select the folder where you saved it and choose Show Info—the Info panel shows the query that is used by Spotlight. Note however, that there are slight differences between the NSPredicate format string and the one stored in Finder. The Finder string might look like the following example.
- 您可以在Finder中从搜索中创建谓词格式字符串。 执行搜索,保存,然后选择保存它的文件夹,然后选择“显示信息” - “信息”面板显示Spotlight使用的查询。 但请注意,NSPredicate格式字符串与存储在Finder中的字符串之间存在细微差别。 Finder字符串可能类似于以下示例。
(((* = "FooBar*"wcd) || (kMDItemTextContent = "FooBar*"cd))
&& (kMDItemContentType != com.apple.mail.emlx)
&& (kMDItemContentType != public.vcard))
Typically all you have to do to convert the Spotlight query into a predicate format string is make sure the predicate does not start with * (this is not supported by NSMetadataQuery when parsing a predicate). In addition, when you want to use a wildcard, you should use LIKE, as shown in the following example.
- 通常,将Spotlight查询转换为谓词格式字符串所需要做的就是确保谓词不以*开头(解析谓词时NSMetadataQuery不支持)。 此外,如果要使用通配符,则应使用LIKE,如以下示例所示。
((kMDItemTextContent LIKE[cd] "FooBar")
&& (kMDItemContentType != "com.apple.mail.emlx")
&& (kMDItemContentType != "public.vcard"))
网友评论