Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/96.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在Swift中找到正确的路径_Ios_Sqlite_Swift_Database Connection - Fatal编程技术网

Ios 如何在Swift中找到正确的路径

Ios 如何在Swift中找到正确的路径,ios,sqlite,swift,database-connection,Ios,Sqlite,Swift,Database Connection,我从文档中获得了这段代码,我找不到文件的路径,我需要将“contacts.db”文件从Supporting Files文件夹复制到设备中的应用程序,而不是模拟器中的应用程序,以便脱机使用 func copyItemAtPath(_ srcPath: String, toPath dstPath: String, error error: NSErrorPointer) -> Bool srcPath=要移动的文件或目录的路径。此参数不能为零。 dst

我从文档中获得了这段代码,我找不到文件的路径,我需要将“contacts.db”文件从Supporting Files文件夹复制到设备中的应用程序,而不是模拟器中的应用程序,以便脱机使用

func copyItemAtPath(_ srcPath: String,
         toPath dstPath: String,
          error error: NSErrorPointer) -> Bool
srcPath=要移动的文件或目录的路径。此参数不能为零。

dstPath=放置srcPath副本的路径。此路径必须在其新位置包含文件或目录的名称。此参数不能为零。

error=输入时,指向错误对象的指针。如果发生错误,此指针将设置为包含错误信息的实际错误对象。如果不需要错误信息,可以为此参数指定nil


非常感谢您的帮助。:)

您只需将“contacts.db”拖放到Project Navigator中,然后您就可以通过以下方式找到该文件的路径:

let sourcePath = NSBundle.mainBundle().pathForResource("contacts", ofType: "db")
var error : NSError?
    NSFileManager().copyItemAtPath(sourcePath!, toPath: destinationPath, error: &error)
    if let error = error {
        println(error.description)
    }
之后,您可以将该文件复制到应用程序的文档文件夹中,为此,您需要该文档文件夹路径:

let doumentDirectoryPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as! String
let destinationPath = doumentDirectoryPath.stringByAppendingPathComponent("contacts1.db")
现在您有了
sourcePath
destinationPath
,因此您可以将该文件复制到文档文件夹中:

NSFileManager().copyItemAtPath(sourcePath!, toPath: destinationPath, error: &error)
如果你想检查错误,你可以这样做:

let sourcePath = NSBundle.mainBundle().pathForResource("contacts", ofType: "db")
var error : NSError?
    NSFileManager().copyItemAtPath(sourcePath!, toPath: destinationPath, error: &error)
    if let error = error {
        println(error.description)
    }
如果您正在获取
sourcePath
nil,则转到目标->YouApp->构建阶段->复制捆绑资源并将文件添加到那里。
希望这能有所帮助。

嗨,达尔梅什,谢谢你的详细回复,我将你的所有代码复制到delegate.swift,但下面有很多错误。我正在使用FMDB有问题吗?错误域=nscocaerorDomain代码=4“操作无法完成。(Cocoa错误4。)”用户信息=0x1740ef600{NSSourceFilePathErrorKey=/private/var/mobile/Containers/Bundle/Application/BCD1A88D-DCAB-4A4D-90F3-B1C0FC99525B/Database-Book.app/contacts.db,NSUserStringVariant=(副本),NSDestinationFilePath=/var/mobile/Containers/Data/Application/B8079789-BEE7-4C3A-A980-c794B0554b6/Documents/contacts.db,NSFilePath=/private/var/mobile/Containers/Bundle/Application/BCD1A88D-DCAB-4A4D-90F3-B1C0FC99525B/Database-Book.app/contacts.db,NSUnderlyingError=0x174059170“操作无法完成。没有这样的文件或目录”}我删除了该文件,并通过构建阶段添加了该文件,但出现了相同的错误:(有什么建议吗?请检查此示例项目):