0

我正在编写一个 OS X 应用程序供我个人使用,我想在其中获取并随后搜索所有 plist 类型的文件。我正在为第一项任务而苦苦挣扎。您能否指出我正确的方向或提供在线教程。谢谢。

4

2 回答 2

0

尝试这样的事情:

NSError *error = nil;
NSString *folder = <# your folder #>;
NSArray *contents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:folder error:&error];
if (error) {
    <# deal with error #>
} else {
    NSString *path;
    for (NSString *entry in contents) {
        if ([entry hasSuffix:@"plist"]) {
            path = [folder stringByAppendingPathComponent:entry];
            <# do something with file at path #>
        }
    }
}

此外,请检查类参考中发现目录内容部分中的相关方法。NSFileManager

于 2012-05-04T15:25:59.870 回答
0
NSString *searchDirectory = ...;
NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath: searchDirectory 
                                                                     error: NULL];
[files enumerateObjectsUsingBlock: ^(NSString *fileName, NSUInteger idx, BOOL *stop) {
    if ([[fileName pathExtension] isEqualToString: @"plist"]) {
        NSString *filePath = [searchDirectory stringByAppendingPathComponent: fileName];
        // Do something with the plist file
    }
}];
于 2012-05-04T15:24:09.237 回答