我在文档目录中有一个问题

时间:2016-06-20 13:13:52

标签: swift nsdocumentdirectory

当我想在文档目录中创建文件夹时,我有问题 当我第一次使文件夹像文件夹(你好),当我尝试制作另一个文件夹像(你好)在小写字母没有做什么和文件夹不创建我不知道为什么?但是,如果我尝试用小空间制作文件夹(你好)它是好的,它显示在表视图上我不知道为什么,如果任何人可以检查文件夹你好和你好不同

请帮助

以下是代码

@IBAction func btnMakeFolder(sender: AnyObject) {
   var checkFoundAlbum:Bool = false // for check the folder if is found it or not

    let c = NSCharacterSet.whitespaceCharacterSet()
    if folderNameTextField.text?.stringByTrimmingCharactersInSet(c) != "" {

        let fileManager = NSFileManager.defaultManager()
        do {

              let document = try fileManager.URLForDirectory(.DocumentDirectory, inDomain: .UserDomainMask, appropriateForURL: nil, create: false)
                // her i check the album is he exists or not
               let getFolders = try fileManager.contentsOfDirectoryAtURL(document, includingPropertiesForKeys: nil, options: .SkipsHiddenFiles)

            for folder in getFolders {
                if folder.lastPathComponent!.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet()) == folderNameTextField.text!.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet()){
                    checkFoundAlbum = true
                }else {
                    checkFoundAlbum = false

                }
            }

            // her for create folder and display the alert for the user
            if checkFoundAlbum == false{

                let folderUrl = document.URLByAppendingPathComponent(folderNameTextField.text!)
                try fileManager.createDirectoryAtURL(folderUrl, withIntermediateDirectories: true, attributes: nil)
                self.dismissViewControllerAnimated(true, completion: nil)

            }else {
                let alertController = UIAlertController(title: "Album Exists", message: "This Album Already Exists,Please Change The Name", preferredStyle: .Alert)
                let alertAction = UIAlertAction(title: "OK", style: .Default, handler: { (alertAction:UIAlertAction) in

                })
                alertController.addAction(alertAction)
                self.presentViewController(alertController, animated: true, completion: nil)
            }

        }catch {
            print(error)
        }

    }else {
        alert()
    }
}

1 个答案:

答案 0 :(得分:0)

我建议在实际设备上尝试此代码 - 假设模拟器的基础文件系统使用HFS +,iOS模拟器通常不区分大小写。