2015-06-18 57 views
0

我已将该文件复制到项目中,并且我是新编写的swift编译器,但我认为该文件将部署到项目中所有资源文件的设备上。这是我的代码将无法找到该文件或从包找不到我的文件

if let dirs = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .AllDomainsMask, true) as ? [String] { 
     let dir = dirs[0] //documents directory 
     let path = dir.stringByAppendingPathComponent(file) 
     var checkValidation = NSFileManager.defaultManager() 

     if checkValidation.fileExistsAtPath(path) { 
     println("FILE AVAILABLE") 
     } else { 
     println("FILE NOT AVAILABLE") 
     // Copy the file from the Bundle and write it to the Device: 
     let pathToBundledDB = NSBundle.mainBundle().pathForResource("pcQuestionDBA", ofType: "txt") 
     var error: NSError ? 
     if NSFileManager.defaultManager().copyItemAtPath(pathToBundledDB!, toPath: dir, error: & error) { 

     } 

我知道我必须做一些根本性的错误和任何建议,将真正的赞赏复制 - 感谢

的更新: 我有将文件复制到项目文件夹中,但是,我删除了该文件并尝试重新复制该文件,并且收到错误消息,指出该文件已存在于该文件夹中。我想我已经试图修复我的错误并找到该文件,将Xcode与我尝试的多种移动和变体相混淆。我重新命名了文件并从头开始,现在一切正常。非常感谢您的快速回复,它让我最终得到了解决方案 - 欢呼声

+0

你将它添加到适当的目标(S)?另外,你是否将其复制到文件夹中? – LinusGeffarth

回答

1

问题在于您应该将路径传递给文件,但您已将路径传递到目录。

尝试这样的:

NSFileManager.defaultManager().copyItemAtPath(pathToBundledDB!, toPath: path, error: &error)