I am trying to create a global array
(of items in a cart) in Swift so that I can access it from different pages.
Can you please point me in the right direction? declaring an array under the import statement in AppDelegate gives me the "unresolved Identifier" issue.
What should I use? How can I accomplish that? Thank you!
In swift you can encapsulate variables in a struct and you can access them anywhere.
struct GlobalVariables {
static var globalString = "MyString"
}
To call it
// Get value
let str = GlobalVariables.globalString
// Set value
GlobalVariables.globalString = "New value"
Checkout the reference for more information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With