macOS SearchKit CoreService一无所获

时间:2018-06-22 08:47:24

标签: xcode macos core-services searchkit

我正在计划对文件分配的内容进行搜索,我计划使用SearchKit,但我不知道该使Apple的示例代码正常工作,也找不到任何其他资源(NSHipster代码也不起作用),这是我的代码:

#define kSearchMax 1000

@interface ViewController()
@property(nonatomic) SKIndexRef mySKIndex;
@end

@implementation ViewController
@synthesize mySKIndex;

- (void)viewDidLoad {
    [super viewDidLoad];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
        [self openIndex];
        [self addDoc];

        SKIndexFlush(self.mySKIndex);

//      i thought that the indexation may need some time ..
        sleep(2);
        dispatch_async(dispatch_get_main_queue(), ^{
            [self searchterm:@"var"];
        });
    });
}


- (void) openIndex {
    NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"index"] stringByAppendingPathExtension:@"txt"];      // 1
    NSURL *url = [NSURL fileURLWithPath:path];

    NSString *name = @"extension_index";
    if ([name length] == 0) name = nil;

    SKIndexType type = kSKIndexInverted;

    if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
        mySKIndex = SKIndexOpenWithURL ((__bridge CFURLRef) url,
                                        (__bridge CFStringRef) name,
                                        true
                                        );
    }else{
        self.mySKIndex = SKIndexCreateWithURL((__bridge CFURLRef) url,
                                              (__bridge CFStringRef) name,
                                              (SKIndexType) type,
                                              (CFDictionaryRef) NULL);
    }
}


- (void) addDoc {
    SKLoadDefaultExtractorPlugIns ();

    NSString *path = [NSBundle.mainBundle pathForResource:@"Products" ofType:@"rtf"];  // 1
    NSURL *url = [NSURL fileURLWithPath: path];          // 2
    SKDocumentRef doc = SKDocumentCreateWithURL ((__bridge CFURLRef) url);
    NSString *mimeTypeHint = @"text/rtf";
    BOOL added = SKIndexAddDocument ((SKIndexRef) mySKIndex,
                                        (SKDocumentRef) doc,
                                        (__bridge CFStringRef)mimeTypeHint,
                                        (Boolean) true
                                        );
    NSLog(added ? @"added" : @"not added");
}

- (void) searchterm:(NSString*)query{
    SKSearchOptions options = kSKSearchOptionDefault;
    BOOL more = YES;
    UInt32 totalCount = 0;
    SKSearchRef search = SKSearchCreate (mySKIndex,
                                         (__bridge CFStringRef) query,
                                         options);
    while (more) {
        SKDocumentID    foundDocIDs [kSearchMax];
        float           foundScores [kSearchMax];
        float *scores;
        Boolean unranked =
        options & kSKSearchOptionNoRelevanceScores;

        if (unranked) {
            scores = NULL;
        } else {
            scores = foundScores;
        }

        CFIndex foundCount = 0;
        more = SKSearchFindMatches (
                                    search,
                                    kSearchMax,
                                    foundDocIDs,
                                    scores,
                                    100,
                                    &foundCount
                                    );
        NSLog(@"%@", [NSString stringWithFormat:@"current count = %i", totalCount]);
        totalCount += foundCount;
    }
}
@end

它总是打印“当前计数= 0”,并且循环只执行一次。

0 个答案:

没有答案