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/