Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(Swift) How to create global array?

Tags:

arrays

swift

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!

like image 835
timtamsyoyo Avatar asked Jan 19 '16 19:01

timtamsyoyo


1 Answers

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.

like image 92
Rashwan L Avatar answered Oct 03 '22 20:10

Rashwan L