我是swift的新手,我正在尝试使用我在文档目录中创建的文件夹中名为“Dates”的文件夹填充一个uipicker。
我可以使用以下代码列出文件:
func listFilesFromDatesFolder() {
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentDirectory = paths[0] as! String
let path = documentDirectory.stringByAppendingPathComponent("Dates")
let manager = NSFileManager.defaultManager()
if let allItems = manager.contentsOfDirectoryAtPath(path, error: nil) {
println(allItems)
}
}
我只是不确定如何将列表放入数组中,我可以在swift中加载选择器。
我之前使用目标c:
在之前的应用中完成了此操作NSFileManager *directoryContent;
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [path objectAtIndex:0];
NSString *contractorDir= [documentsDirectory stringByAppendingPathComponent:contractorTwo.text];
NSError *err;directoryContent = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:contractorDir error: err];
NSArray *sorted = [directoryContent sortedArrayUsingSelector:@selector(compare:)];
我希望有人可以提供帮助,如果可以,请提前感谢你!
干杯
答案 0 :(得分:0)
查看此示例代码:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let (filenamesOpt, errorOpt) = contentsOfDirectoryAtPath("/Users")
if let filenames = filenamesOpt {
println(filenames) // [".localized", "kris", ...]
}
let (filenamesOpt2, errorOpt2) = contentsOfDirectoryAtPath("/NoSuchDirectory")
if let err = errorOpt2 {
err.description // "Error Domain=NSCocoaErrorDomain Code=260 "The operation couldn’t be completed. ... "
}
getContentsOfDirectoryAtPath("/Users") { (result, error) in
if let e = error {
e.description
}
else if let filenames = result {
filenames // [".localized", "kdj", ... ]
}
}
getContentsOfDirectoryAtPath("/NoSuchDirectory") { (result, error) in
if let e = error {
e.description // "Error Domain=NSCocoaError..."
}
else if let filenames = result {
filenames
}
}
// Do something with the files in my home directory
let listMyFiles = { block in self.getContentsOfDirectoryAtPath("/Users/kdj", block: block) }
listMyFiles { (result, error) in
if let e = error {
e.description
}
else if let filenames = result {
filenames // [".bashrc", ... ]
}
}
}
// Tuple result
// Get contents of directory at specified path, returning (filenames, nil) or (nil, error)
func contentsOfDirectoryAtPath(path: String) -> (filenames: [String]?, error: NSError?) {
var error: NSError? = nil
let fileManager = NSFileManager.defaultManager()
let contents = fileManager.contentsOfDirectoryAtPath(path, error: &error)
if contents == nil {
return (nil, error)
}
else {
let filenames = contents as! [String]
return (filenames, nil)
}
}
// Continuation Passing Style (CPS) interface
// Get contents of directory at specified path, invoking block with (filenames, nil) or (nil, error)
func getContentsOfDirectoryAtPath(path: String, block: (filenames: [String]?, error: NSError?) -> ()) {
var error: NSError? = nil
let fileManager = NSFileManager.defaultManager()
let contents = fileManager.contentsOfDirectoryAtPath(path, error: &error)
if contents == nil {
block(filenames: nil, error: error)
}
else {
let filenames = contents as! [String]
block(filenames: filenames, error: nil)
}
}
}
让我知道它是否适合你。
来自HERE的参考。
答案 1 :(得分:0)
非常感谢你!
这个代码示例很有效!
https://github.com/DharmeshKheni/Show-Document-Directory
我在选择器中显示文件列表的唯一主要变化是,我必须将我的选择器委托连接到视图控制器:
类ViewTwo:UIViewController, UIPickerViewDelegate {
.......然后在'ViewDidLoad'之后声明它:
filePicker.delegate = self
UiPickerDataSource不是必需的,当我尝试将它连接到viewController时抛出异常。
再次感谢!