2015-06-30 52 views
1

我是swift的新手,並試圖用我在我的文檔目錄中創建的名爲「Dates」的文件夾中的文件名填充uipicker。使用contentsOfDirectory填充uipicker Swift

我可以列出使用該代碼的文件:

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) 
     } 
} 

我只是不確定如何獲取列表到一個數組,我可以迅速與加載選擇器。

我以前在以前的應用程序中使用Objective 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

看看這個例子代碼:

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

+0

嗨,謝謝你。實際上,我查看了以前提供的示例代碼,它並沒有真正幫助我將文件名獲取到可以加載選擇器的數組中。這就是我現在如何加載picker來測試它:let pickerData = [「One」,「two」,「three」,「four」] – Cableguy69

+0

func numberOfComponentsInPickerView(pickerView:UIPickerView) - > Int { return 1 } FUNC pickerView(pickerView:UIPickerView,numberOfRowsInComponent成分爲:int) - >詮釋{ 回報pickerData.count } FUNC pickerView(pickerView:UIPickerView,titleForRow行:詮釋,forComponent成分爲:int) - >字符串! { 返回pickerData [行] } FUNC pickerView(pickerView:UIPickerView,didSelectRow行:中等,inComponent組分的:int){ standingCharge。text = pickerData [row] – Cableguy69

+0

我只需要知道如何從文件列表創建一個數組,然後將其加載到選取器中,類似於上面代碼中名爲'pickerData'的預填充數組。 – Cableguy69

0

非常感謝!

此代碼示例工作得很好!

https://github.com/DharmeshKheni/Show-Document-Directory

爲我展現在選擇器的文件列表中的唯一的主要變化,是我有我的選擇器的委託連接到視圖控制器:

類ViewTwo:UIViewController中,UIPickerViewDelegate {

.......然後「ViewDidLoad'like此之後聲明它:

filePicker.delegate =自

UiPickerDataSource不是必需的,並且在嘗試將其連接到viewController時拋出異常。

再次感謝!