NSMetadataQuery数据库

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了NSMetadataQuery数据库相关的知识,希望对你有一定的参考价值。

  1. (null)/(null)/(null) (null) | Permalink
  2. NSMetadataQuery seems to have bugs and unable to get NSMetadataQueryResultContentRelevanceAttribute value.
  3.  
  4. Use MDQueryRef instead.
  5.  
  6.  
  7.  
  8. #import
  9.  
  10. @interface MyDocument : NSDocument
  11. {
  12. IBOutlet id field;
  13.  
  14. MDQueryRef _query;
  15.  
  16.  
  17. }
  18. - (IBAction)search:(id)sender;
  19. @end
  20.  
  21. ---------
  22.  
  23. #import "MyDocument.h"
  24.  
  25.  
  26. @implementation MyDocument
  27.  
  28. - (id)init
  29. {
  30. self = [super init];
  31. if (self) {
  32. }
  33. return self;
  34. }
  35.  
  36. - (NSString *)windowNibName
  37. {
  38. return @"MyDocument";
  39. }
  40.  
  41. - (void)windowControllerDidLoadNib:(NSWindowController *) aController
  42. {
  43. [super windowControllerDidLoadNib:aController];
  44.  
  45. }
  46.  
  47.  
  48. - (IBAction)search:(id)sender
  49. {
  50.  
  51. if( [[field stringValue] isEqualToString:@"" ] ) return;
  52.  
  53.  
  54. NSString *predicateFormat = @"(kMDItemTextContent == "?%@*"c) && (kMDItemContentType != 'com.apple.mail.emlx') && (kMDItemContentType != 'public.vcard')";
  55.  
  56. _query = MDQueryCreate (
  57. NULL,
  58. [NSString stringWithFormat:predicateFormat,[field stringValue]],
  59. [NSArray arrayWithObjects:kMDQueryResultContentRelevance,
  60. kMDItemPath, kMDItemDisplayName, kMDItemFSName,nil ],
  61. [NSArray arrayWithObject:kMDQueryResultContentRelevance]
  62. );
  63.  
  64.  
  65. MDQuerySetSearchScope (
  66. _query,
  67. [NSArray arrayWithObject:kMDQueryScopeComputer ],
  68. 0
  69. );
  70.  
  71.  
  72. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(finish) name:kMDQueryDidFinishNotification object:_query];
  73.  
  74.  
  75.  
  76. MDQueryExecute (
  77. _query,
  78. kMDQuerySynchronous
  79. );
  80.  
  81.  
  82.  
  83. }
  84.  
  85.  
  86. -(void)finish
  87. {
  88.  
  89. [[NSNotificationCenter defaultCenter] removeObserver:self
  90. name:nil
  91. object:_query];
  92.  
  93. MDItemRef miref;
  94.  
  95. CFIndex idx;
  96.  
  97.  
  98. idx = MDQueryGetResultCount(_query);
  99.  
  100.  
  101. CFIndex hoge;
  102. for( hoge = 0; hoge < idx; hoge++ )
  103. {
  104.  
  105. miref = MDQueryGetResultAtIndex( _query, hoge);
  106.  
  107.  
  108. NSString* name = (NSString*)MDItemCopyAttribute (
  109. miref,
  110. kMDItemFSName
  111. );
  112. NSString* path = (NSString*)MDItemCopyAttribute (
  113. miref,
  114. kMDItemPath
  115. );
  116. NSString* displayname = (NSString*)MDItemCopyAttribute (
  117. miref,
  118. kMDItemDisplayName
  119. );
  120. NSNumber* score = (NSNumber*)MDQueryGetAttributeValueOfResultAtIndex (
  121. _query,
  122. kMDQueryResultContentRelevance,
  123. hoge
  124. );
  125.  
  126. if( score != nil )
  127. NSLog(@"score %f",[score floatValue]);
  128.  
  129.  
  130. }
  131.  
  132.  
  133. }
  134.  
  135. @end

以上是关于NSMetadataQuery数据库的主要内容,如果未能解决你的问题,请参考以下文章

禁用 iCloud 时是不是可以使用 NSMetadataQuery?

NSMetadataQuery 忽略自定义文件包类型

UIDocument openWithCompletionHandler:强制 NSMetadataQuery 更新

如何限制从 NSMetadataQuery 返回的结果数量?

为啥检查 [NSMetadataQuery 类] 在 iOS 4.2.1 上崩溃?

使用 NSMetadataQuery 在目录中搜索文件