gpt4 book ai didi

ios - Swift - 如何检查现有的 Core Data Store iOS

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

我正在开发一个应用程序,它应该有一个启动窗口,该窗口将有一个介绍,如果没有找到现有的核心数据存储,然后是一个“开始”按钮,如果存在一个存储 - 它应该有“继续 session ”和“重新开始”按钮。唯一的问题是我认为我的商店检查代码没有工作——我把它放在 ViewController 的 viewDidLoad 函数中:

override func viewDidLoad() {
super.viewDidLoad()
let defaultStorePath = NSBundle.mainBundle().pathForResource("LearnUSStateCapitals", ofType: "sqlite")
let fileManager = NSFileManager.defaultManager()
if defaultStorePath == nil {
println("can't find store")
generateSCA()
} else {
println("store found...I think")
continueButton.hidden = false
}

所以我得到的只是“找不到商店”- 即使在运行该应用程序几次之后也是如此。我已经导航到 \Library\Developer\CoreSimulator\Devices\blahblah\data\Containers\Data\Application\blahblah\Documents 并确实看到了我当时创建的“LearnUSStateCapitals.sqlite”文件首先按原样运行应用程序...

我使用 if defaultStorePath == nil 的原因是因为当我尝试 if fileManager.fileExistsAtPath(defaultStorePath!) 我得到 unexpectedly found nil while展开可选值 - 为什么这个 nil 可能是真正的问题...感谢您的帮助

最佳答案

您可以看到的文件位于Application Documents 目录中,与主包不同。您可以获得应用程序文档目录的 URL:

let urls = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)
let appDocsURL : NSURL = urls[urls.count - 1] as NSURL
let defaultStoreURL = appDocsURL.URLByAppendingPathComponent("LearnUSStateCapitals.sqlite")

然后使用:

if !fileManager.fileExistsAtPath(defaultStoreURL.path!) {
println("can't find store")
generateSCA()
} else {
println("store found...I think")
continueButton.hidden = false
}

关于ios - Swift - 如何检查现有的 Core Data Store iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28179713/

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