非空表的数据

发布于 2025-02-11 05:24:51 字数 1398 浏览 2 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三生殊途 2025-02-18 05:24:51

好吧,我通常要知道我的应用程序是否首次运行的一件事是在用户默认值中存储一个值。您还可以存储应用程序已有的执行次数,然后根据此值实现逻辑。在这里,我像我一样离开你。

要与用户默认工作,我使用的是很棒的库:

swiftyuserdefaults )

userDefaults.swift

此文件中的swift放置此代码:

import Foundation
import SwiftyUserDefaults

// Define app user defaults keys
extension DefaultsKeys {
    var isFirstAppLaunch: DefaultsKey<Bool> { .init("isFirstAppLaunch", defaultValue: true) }
}

接下来,在您的视图控制器ViewDidload中您可以实现以下操作:

import SwiftyUserDefaults

override func viewDidLoad() {
    super.viewDidLoad()

    self.fillDataInFirstAppLaunch()
}

func fillDataInFirstAppLaunch() {
    // If first execution, then fill CoreData with default values
    if Defaults.isFirstAppLaunch {
        // Set to false to run this code only once (the first app launch)
        Defaults.isFirstAppLaunch = false
            
        // Implement your logic here to fill CoreData
    }
}

Well one of the things that I normally do to know if my application is running for the first time is to store a value in User Defaults. You can also store the number of executions your application has had and then implement your logic based on this value. Here I leave you as I would.

To work with the User Defaults I use this library that is great: SwiftyUserDefaults

Create a file named (for example)

UserDefaults.swift

Inside this file put this code:

import Foundation
import SwiftyUserDefaults

// Define app user defaults keys
extension DefaultsKeys {
    var isFirstAppLaunch: DefaultsKey<Bool> { .init("isFirstAppLaunch", defaultValue: true) }
}

Next, in your view controller viewDidLoad you can implement this:

import SwiftyUserDefaults

override func viewDidLoad() {
    super.viewDidLoad()

    self.fillDataInFirstAppLaunch()
}

func fillDataInFirstAppLaunch() {
    // If first execution, then fill CoreData with default values
    if Defaults.isFirstAppLaunch {
        // Set to false to run this code only once (the first app launch)
        Defaults.isFirstAppLaunch = false
            
        // Implement your logic here to fill CoreData
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文