OS X API 枚举包含 AppleDouble 文件(以“._”开头)的文件夹?
OS X API to enumerate a folder including AppleDouble files (beginning with a "._")?
以下 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
}
以下 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
}