gpt4 book ai didi

swift - 升级我的应用程序以包含预填充的数据

转载 作者:行者123 更新时间:2023-11-28 08:54:46 25 4
gpt4 key购买 nike

我决定改进我的初始设计并预填充我的应用程序的某些部分。我正在尝试按照本教程阅读 sqlite:http://www.appcoda.com/core-data-preload-sqlite-database/

但是这行代码是这样的:

if !NSFileManager.defaultManager().fileExistsAtPath(url.path!) {
let sourceSqliteURLs = [NSBundle.mainBundle().URLForResource("CoreDataDemo", withExtension: "sqlite")!, NSBundle.mainBundle().URLForResource("CoreDataDemo", withExtension: "sqlite-wal")!, NSBundle.mainBundle().URLForResource("CoreDataDemo", withExtension: "sqlite-shm")!]

let destSqliteURLs = [self.applicationDocumentsDirectory.URLByAppendingPathComponent("CoreDataDemo.sqlite"),
self.applicationDocumentsDirectory.URLByAppendingPathComponent("CoreDataDemo.sqlite-wal"),
self.applicationDocumentsDirectory.URLByAppendingPathComponent("CoreDataDemo.sqlite-shm")]

var error:NSError? = nil
for var index = 0; index < sourceSqliteURLs.count; index++ {
NSFileManager.defaultManager().copyItemAtURL(sourceSqliteURLs[index], toURL: destSqliteURLs[index], error: &error)
}
}

不太适合我的应用程序,因为我使用的是共享应用程序文件夹。我面临的问题是应用程序加载正常并通过了代码行,但是当我查看显示信息的页面时,什么也没有。我下载了 sqlite 阅读器来查看数据库中是否有对象,所以我知道这不是问题所在。我必须对这些代码行进行哪些修改才能使共享应用程序组正确执行此功能?共享应用组的目录是这样的:

let directory = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("group.groupname.appname")

我正在努力寻找与此相关的任何信息,我们将不胜感激。

最佳答案

也许我在你的问题中遗漏了一些东西,但如果你只想更改目标目录,那么只需更改这些行...

let destSqliteURLs = [
self.applicationDocumentsDirectory.URLByAppendingPathComponent("CoreDataDemo.sqlite"),
self.applicationDocumentsDirectory.URLByAppendingPathComponent("CoreDataDemo.sqlite-wal"),
self.applicationDocumentsDirectory.URLByAppendingPathComponent("CoreDataDemo.sqlite-shm")]

进入这个...

let baseURL = NSFileManager.defaultManager().containerURLForSecurityApplicationGroupIdentifier("group.groupname.appname")
let destSqliteURLs = [
baseURL.URLByAppendingPathComponent("CoreDataDemo.sqlite"),
baseURL.URLByAppendingPathComponent("CoreDataDemo.sqlite-wal"),
baseURL.URLByAppendingPathComponent("CoreDataDemo.sqlite-shm")]

关于swift - 升级我的应用程序以包含预填充的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33379092/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com