如何从Documents / Inbox / Swift 3中选择.zip文件

时间:2017-09-06 12:36:15

标签: ios swift3

如何从Documents / Inbox /目录中选择zip文件。同时将所选文件引用到变量。

let filemanager:FileManager = FileManager()
    let files = filemanager.enumerator(atPath: NSHomeDirectory())
    while let file = files?.nextObject() {
        print("Files::",file)
} 

o/p:  
 Files:: Documents/Inbox
 Files:: Documents/Inbox/abc.zip
 Files:: Documents/Inbox/pqrs.zip
 Files:: Documents/Inbox/xyz.zip
 Files:: Library
 Files:: Library/Caches
 Files:: Library/Caches/Snapshots
 Files:: Library/Preferences

我需要从此列表中选择zip文件。

1 个答案:

答案 0 :(得分:0)

swift 3

    let filemanager:FileManager = FileManager()
    let files = filemanager.enumerator(atPath: NSHomeDirectory())
    while let file = files?.nextObject() {

        //print("Files::",file)

        if (String(describing: file).characters.count >= 4){

            let fileName = file as! String

            if fileName.substring(from:fileName.index(fileName.endIndex, offsetBy: -4)) == ".zip"{

                print((file as! NSString).lastPathComponent)
                self.arrayZipfile.append((file as! NSString).lastPathComponent)
            }
        }
    }

选择arrayZipFile索引。