用 contentsOfDirectory Swift 填充 uipicker
Posted
技术标签:
【中文标题】用 contentsOfDirectory Swift 填充 uipicker【英文标题】:populate uipicker with contentsOfDirectory Swift 【发布时间】:2015-06-30 09:09:12 【问题描述】:我是 swift 新手,我正在尝试使用我在文档目录中创建的名为“日期”的文件夹中的文件名填充 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)
我只是不确定如何将列表放入可以快速加载选择器的数组中。
我之前在以前的应用程序中使用过目标 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:)];
我希望有人可以帮助解决这个问题,如果可以的话,请提前感谢您!
干杯
【问题讨论】:
【参考方案1】:查看此示例代码:
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的参考。
【讨论】:
您好,谢谢。我实际上已经查看了之前提供的示例代码,它并没有真正帮助我将文件名放入一个可以加载选择器的数组中。这就是我现在加载选择器来测试它的方式: let pickerData = ["One","two","three","four"] func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int return 1 func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int return pickerData.count func pickerView(pickerView: UIPickerView, titleForRow 行:Int,forComponent 组件:Int) -> String! return pickerData[row] func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) standingCharge.text = pickerData[row] 我只需要知道如何从文件列表中创建一个数组,然后将其加载到选择器中,类似于上面代码中名为“pickerData”的预填充数组。 查看这个示例项目:github.com/DharmeshKheni/Show-Document-Directory【参考方案2】:非常感谢!
这个代码示例很好用!
https://github.com/DharmeshKheni/Show-Document-Directory
我在选择器中显示文件列表的唯一主要变化是我必须将我的选择器委托连接到视图控制器:
类 ViewTwo:UIViewController,UIPickerViewDelegate
.......然后像这样在'ViewDidLoad'之后声明它:
filePicker.delegate = self
UiPickerDataSource 不是必需的,当我尝试将它连接到 viewController 时抛出异常。
再次感谢!
【讨论】:
以上是关于用 contentsOfDirectory Swift 填充 uipicker的主要内容,如果未能解决你的问题,请参考以下文章