1

我正在从 iCloud 获取数组中的文件列表,我希望这些文件使用NSMetadataItemFSCreationDateKey或进行排序NSMetadataItemFSContentChangeDateKey。但它并没有按照给定的方式返回NSSortDescriptor

这是我的代码,如果缺少任何内容或我需要添加任何内容,请建议我。

-(void)getiCloudData
{

[appdel.metadataQuery setSearchScopes:[NSArray arrayWithObjects: NSMetadataQueryUbiquitousDocumentsScope,NSMetadataQueryUbiquitousDataScope ,nil]];
[appdel.metadataQuery setPredicate:[NSPredicate predicateWithFormat:@"%K LIKE '*'", NSMetadataItemFSNameKey]];

NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:NSMetadataItemFSCreationDateKey
                             ascending:FALSE] autorelease];
NSArray *sortDescriptors = [NSArray arrayWithObjects:
                            sortDescriptor,
                            nil];
[appdel.metadataQuery setSortDescriptors:sortDescriptors];

[[NSNotificationCenter defaultCenter] addObserver:self
                                     selector:@selector(queryDidReceiveNotification:)
                                         name:NSMetadataQueryDidFinishGatheringNotification
                                       object:appdel.metadataQuery];
[[NSNotificationCenter defaultCenter] addObserver:self
                                     selector:@selector(updatequeryDidReceiveNotification:)
                                         name:NSMetadataQueryDidUpdateNotification
                                       object:appdel.metadataQuery];
   [appdel.metadataQuery enableUpdates];
   [appdel.metadataQuery startQuery];
}

谢谢。

4

1 回答 1

1

我相信NSMetadataItemFSCreationDateKey总是返回为零,看到这个,所以甚至不要尝试排序。

现在,我不按 排序NSMetadataItemFSContentChangeDateKey,尽管当查询结果通过时,我确实使用 valueForAttribute: 从 NSMetadataItem 查询它,并且此时工作正常。所以把你的精力集中在NSMetadataItemFSContentChangeDateKey.

If you can't get the query results to come through sorted, one possible approach is to sort the results after you get them through - when you're processing your query results. I've used that when I've had an unusual sorting requirement in the past.

于 2013-12-01T08:26:45.940 回答