以下 4 个 NSFileManager API 无法枚举以“._”开头的 AppleDouble 文件
enumeratorAtPath, 枚举器在 URL, contentsOfDirectoryAtPath, contentsOfDirectoryAtURL
应该使用哪个 API 来枚举它们而不会失败?
最佳答案
下面的工作,以防万一有人需要
func contentsOfDirectoryUsingCAPI() -> [String]? {
var contentsOfDirectory: [String]?
if let folderPathCString = fileURL.path!.cStringUsingEncoding(NSUTF8StringEncoding) {
// Open the directory
let dir = opendir(folderPathCString)
if dir != nil {
contentsOfDirectory = []
// Use readdir to get each element
var entry = readdir(dir)
while entry != nil {
let d_namlen = entry.memory.d_namlen
let d_name = entry.memory.d_name
// dirent.d_name is defined as a tuple with
// MAXNAMLEN elements. We want to convert
// that to a null-terminated C string.
var nameBuf: [CChar] = Array()
let mirror = Mirror(reflecting: d_name)
for _ in 0..<d_namlen {
for child in mirror.children {
if let value = child.value as? CChar {
nameBuf.append(value)
}
}
}
// Null-terminate it and convert to a String
nameBuf.append(0)
if let name = String.fromCString(nameBuf) {
contentsOfDirectory?.append(name)
}
entry = readdir(dir)
}
closedir(dir)
}
}
return contentsOfDirectory
}
关于swift - OS X API 枚举包含 AppleDouble 文件的文件夹(以 "._"开头)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33762567/