gpt4 book ai didi

ios - 更新应用程序时替换预加载的数据 - iOS Swift

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

我有一个应用程序在第一次启动时将 CSV 文件预加载到核心数据中,如下所示:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

let isPreloaded = defaults.bool(forKey: "isPreloaded")
if !isPreloaded {
preloadData()
defaults.set(true, forKey: "isPreloaded")
}
}

但是,我想在更新应用程序时预加载新的更新 CSV 文件。我想我可以在默认值中创建一个名为“isPreloaded2”的新 key ,但 CSV 文件会随着每次更新而更改,我想知道是否有更好的方法。

最佳答案

感谢 JoRa 提供的解决方案。对于任何感兴趣的人,以下是我在 Swift 中的实现方式:

swift 3.0

let defaults = UserDefaults.standard
if let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String {
let currentVersion = defaults.value(forKey: "currentVersion") as? String
if version != currentVersion { //currentVersion will either be nil or last version number when app is launched after update.
preloadData()
defaults.set(version, forKey: "currentVersion") //update currentVersion in defaults so version will match currentVersion next time.
}
}

并且不要忘记在预加载新数据之前删除旧数据(我的 preloadData() 函数包含一个 removeData() 函数)。

关于ios - 更新应用程序时替换预加载的数据 - iOS Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42727020/

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