swift - OS X API 枚举包含 AppleDouble 文件的文件夹(以 "._"开头)?

标签 swift macos api nsfilemanager hidden-files

以下 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/

相关文章:

macos - Swift 中的 CommonHMAC

python - MacOSX Instruments 分析 Python 代码

html - 设置送货地址捐赠

javascript - 尝试在 React.js 中从另一个图像服务器(akamai)获取图像

ajax - 使用 API 和 AJAX 发送/包含 'name' 字段到 Mailchimp

swift - 如何将数据从一个 View Controller 传递到另一个 View Controller ?

ios - 使用 CTCallCenter (Swift) 在 iOS 上检测电话

ios - 在 Swift 中分配来自其他类的函数

ios - 在 iPhone 上获取当前在 Spotify 中播放的歌曲

macos - 如何在 Mac OS X Lion 上安装 Sqlite3