Swift Core Data - 更改持久存储的位置

问题描述 投票:0回答:1

我有一个 swift 程序可以读取 CSV 文件并将数据保存到核心数据持久存储中。我想更改持久存储的位置。根据 Apple 文档,我似乎可以通过覆盖 NSPersistentContainer 的子类中的默认目录来做到这一点。以下是我不成功的尝试。 Xcode 错误是“无法调用非函数类型 URL 的值”。

final class CoreDataContainer: NSPersistentContainer {
    let storePathURL: URL = URL(string: "file:///Users/Chris/Developer/Persistent Store")!
    override class func defaultDirectoryURL() -> URL {
        return super.defaultDirectoryURL()
            .absoluteURL(storePathURL)
   }
}
swift core-data nspersistentcontainer
1个回答
0
投票

不确定这是否是您要问的:

let storePathURL = // ...
let description = NSPersistentStoreDescription(url: storePathURL)
let container = NSPersistentContainer(name: "YourAppName")

container.persistentStoreDescriptions = [description]

container.loadPersistentStores { _, error in
   // ...
}
© www.soinside.com 2019 - 2024. All rights reserved.