xcode 6.2 每次运行应用程序时都会创建一个新的模拟器路径 [英] xcode 6.2 create a new simulator path every time when run the app

查看:32
本文介绍了xcode 6.2 每次运行应用程序时都会创建一个新的模拟器路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 swiftData 将信息存储在 sqlite 数据库上,直到今天它都能正常工作,突然间它创建了一个新的数据库位置以及模拟器位置第一次运行时显示

I used swiftData to store information on sqlite database till today it will work fine and suddenly it created a new location of database as well as simulator location when first time run it shows

/Users/div/Library/Developer/CoreSimulator/Devices/606D7F8E-2402-4782-ADEE-12725EDB203A/data/Containers/Data/Application/2DB733AF-2544-4256-B1E5-5E872.db

第二次

/Users/div/Library/Developer/CoreSimulator/Devices/606D7F8E-2402-4782-ADEE-12725EDB203A/data/Containers/Data/Application/551991FA-392A-40E9-810D3E-36CEAFC.db

第三次

/Users/div/Library/Developer/CoreSimulator/Devices/606D7F8E-2402-4782-ADEE-12725EDB203A/data/Containers/Data/Application/03E4BE03-D6E7-47BF-A98D-A1228DF0Documents.db

我使用了这个代码

  let fileManager = NSFileManager()
        var Sourcepath = NSBundle.mainBundle().resourcePath?.stringByAppendingPathComponent("PhotoKeeper.db");
        let docsPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as String
        let databaseStr = "DataBase.db"
        let dbPath = docsPath.stringByAppendingPathComponent(databaseStr)
        println(dbPath)    
        if(fileManager .fileExistsAtPath(dbPath) == false) {


            var error:NSError?
            fileManager.copyItemAtPath(Sourcepath!, toPath: dbPath, error: &error)
            println(error)

        }

我的问题是每次创建数据库的新实例时,即如果我插入 2 行并编译并运行应用程序,它将创建零行的新数据库

my problem is every time new instance of database is created i.e. if i insert 2 row and compile and run the app it will create new database with zero row

推荐答案

您应该只保存内容的相对路径(文档、数据库、...),因为无论何时构建和运行它.

You should save only relative path of your contents (document, db, ...) cause XCode with iOS8 (I suppose) changes application folder whenever you build and run it.

例如,您在文档中有 .db 文件,因此只需将链接保存为DataBase.db".然后,当你需要访问这个文件时,获取路径为:

For example, you have your .db file in Documents, so just save your link as "DataBase.db". Then, when you need to access to this file, get the path as:

let documentsFolder = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as String
let dbPath = documentsFolder.stringByAppendingPathComponent("Database.db")

这篇关于xcode 6.2 每次运行应用程序时都会创建一个新的模拟器路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆