How do I make a variable accessible across my project?

I need a variable that will be accessible from my main target, and an imported Framework. A Global Variable doesn't work as it's range is only within the target. I'm not finding anyting online that will help me. Does anyone have any ideas on how to do this? In Swift please.

Accepted Reply

I'm sorry, I left out one thing. Symbols have a default access of "internal", which excludes from from visibility outside their module. You need to declare "myGlobal" public:


public var myGlobal = 0

Replies

Actually, a global variable is accessible globally, but when you want to access it across module boundaries, you must arrange to make the name visible outside its original location. For example, if you have a framework MyFramework with this top-level declaration:


var myGlobal = 0


(so, not inside any class or struct) then you can access it in an app, once you import the module:


import MyFramework
class MyClass {
     func printThatThing () {
          print (myGlobal) // or 'print (MyFramework.myGlobal)' if the name conflicts with a global in your app
     }
}


However, DO NOT DO THIS! 🙂


— If you must have this kind of construct, it's better to use a static within a class/struct in the framework:


struct UsefulValues {
     static var myGlobal = 0
}


That's because this give it at least a bit of context (from the name of the struct) about what it's for, and what else it's used with.


— A long history of computer science has deprecated the use of global variables generally, because they tend to lock your code into patterns you can't change. That is, it's use in one place locks down the meaning and availability in other places, and suddenly your code has lots of dependencies that get in your way.


In most cases, there's is a better approach, where the data is still available across module boundaries, but isn't locked into the rigid structure of globals.

Wierd, this isn't working. This is what my project looks like:


//This is my framework
var myGlobal = 0

import Foundation

public func myFunction(){...


//This is my class which uses the framework
import myFramework

class ViewController: UIViewController{

     func doThis(){
          if myGlobal == 0{
          print ("your global is 0")
          }
         
          else{
          print ("your global isn't 0")
          }
    }
}



In this setup, I get the error "use of unresolved identifier 'myGlobal'" at line 7.


Now, I'm trying to use a global variable because a function in my Framework has to be able to access and change it. I could use a UserDefault, but isn't it better to use variables?

>I could use a UserDefault, but isn't it better to use variables?


It depends on what you want to store, how much, how secure it needs to be and why. user defaults is typically meant for storing user preferences, as an example. You can try to use it for other things, but you probably shouldn't unless you're clear about what is being stored. If global vars are giving you grief, tho, you can try it & see.

I'm sorry, I left out one thing. Symbols have a default access of "internal", which excludes from from visibility outside their module. You need to declare "myGlobal" public:


public var myGlobal = 0

it is probably just a typo, but at line 7 you wrote = instead of ==

That was all. So I was doing it right, but I forgot that by default variables are private. Thanks Quincy!

Yup, my bad. Just fixed it😊