I have an array inside a class called Anacii and obviously an AppDelegate class. I'm trying to save two arrays, both located inside the Anacii class, to UserDefaults
when the application terminates. Everything works fine except getting the two arrays from the Anacii class from the AppDelegate class. Both arrays have multiple values inside them (I tested that with some print statements) and I can access them from my root view controller just fine with all the values inside of them but they return as empty arrays when I get them from the AppDelegate class.
Here are the two arrays defined in the Anacii class:
class Anacii {
// MARK: - Anacs / Rarities
var anacs = [String]()
var rarities = [Int]()
...
}
Here's where I set the actual values inside my root view controller:
class HomeController: UITableViewController, CLLocationManagerDelegate {
private let a = Anacii()
...
override func viewDidLoad() {
super.viewDidLoad()
for _ in 1...10 {
collectAnac()
}
...
}
...
// MARK: - Other functions
func collectAnac() {
let rarity = a.generateRarity()
let anac = a.findAnac(rarity: rarity)
a.anacs.append(anac)
a.rarities.append(rarity)
...
}
}
And finally, here is where I try to access the variables from the AppDelegate class:
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
...
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
let a = Anacii()
let anacs = a.anacs // Comes out as []
let rarities = a.rarities // Comes out as []
...
}
}
The two values at the bottom of the AppDelegate class (anacs and rarities) both equal []
(tested by using print statements).
Sorry if this is a duplicate question, I looked at a lot of other posts like this but could find no answer that worked for me. Thanks!
TL;DR: You should read the whole thing but... Trying to access two arrays from the Anacii class from the AppDelegate class returns empty list, even though those two arrays are NOT empty (other classes see them with all the values they have). See the code above. Sorry if this is a duplicate post.