ios - 接收我们在用户登录时存储的用户默认值(swift3 ...)

标签 ios swift

1) 记录后我从数据库中检索值(确定)

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.

        // Receive user defaults which we stored when user logged in

    let defaults = UserDefaults.standard
    let firstname = defaults.string(forKey: "usernameValue")
    let lastname = defaults.string(forKey: "nameValue")
    let connect_email = defaults.string(forKey: "emailValue")
    let event_mois = defaults.array(forKey: "event_mois")
    }

2) 在 viewDidLoad() 之后,我制作了一个数组来提供显示器(OK)

var sections = [
    Section(genre: "🦁 Event du mois",
            movies: ["The Incredibles", "The Incredibles"],
            expanded: false),
    Section(genre: "💥 Top Event",
            movies: ["Guardians of the Galaxy", "The Flash", "The Avengers", "The Dark Knight"],
            expanded: false),
    Section(genre: "👻 Event Fribourg",
            movies: ["The Walking Dead", "Insidious", "Conjuring"],
            expanded: false)
]

3) 我现在想把我从数据库中检索到的 event_mois 表放在 var 部分中。但是我不能在 viewDidLoad 之外使用这个数组,我也不能把 var 部分放在 viewDidload 中。

有人可以帮助我吗? 谢谢

最佳答案

您不能在其他方法中使用该数组,因为它将超出范围。 解决方案:将数组定义为一个属性,这样你就可以在类中的任何地方使用它,属性在所有方法之外的类中。

class YourClass {

    var sections : [Section] = []

    override func viewDidLoad() {
        super.viewDidLoad()
       // your code
    }
}

关于ios - 接收我们在用户登录时存储的用户默认值(swift3 ...),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45757610/

相关文章:

ios - 如何实现嵌套 Collection View ?

ios - main 方法和 block 中的内存泄漏

iphone - Xcode-使用UIDatePicker时以葡萄牙语显示月份名称

ios - fatal error : unexpectedly found nil while unwrapping an Optional value play audio files

swift - 无法将文本字段的值设置为 Firebase 搜索结果 - Swift

objective-c - 在 Objective C 中使用正则表达式从 url 获取 Vimeo Clip_id

ios - Objective C - 单例静态方法覆盖不适用于多个子类

ios - 观看连接 session 问题

swift - 文档目录 : delete file and rename remaining files in Swift 4. 2

swift - 如何在 Swift 中保存 PDF currentDestination