How to create a global variable? - swift

I have a global variable that needs to be shared among my ViewControllers.
In Objective-C, I can define a static variable, but I can't find a way to define a global variable in Swift.
Do you know of a way to do it?

From the official Swift programming guide:
Global variables are variables that are defined outside of any
function, method, closure, or type context. Global constants and
variables are always computed lazily.
You can define it in any file and can access it in current module anywhere.
So you can define it somewhere in the file outside of any scope. There is no need for static and all global variables are computed lazily.
var yourVariable = "someString"
You can access this from anywhere in the current module.
However you should avoid this as Global variables are not good for application state and mainly reason of bugs.
As shown in this answer, in Swift you can encapsulate them in struct and can access anywhere.
You can define static variables or constant in Swift also. Encapsulate in struct
struct MyVariables {
static var yourVariable = "someString"
}
You can use this variable in any class or anywhere
let string = MyVariables.yourVariable
println("Global variable:\(string)")
//Changing value of it
MyVariables.yourVariable = "anotherString"

Global variables that are defined outside of any method or closure can be scope restricted by using the private keyword.
import UIKit
// MARK: Local Constants
private let changeSegueId = "MasterToChange"
private let bookSegueId = "MasterToBook"

if you want to use it in all of your classes you can use:
public var yourVariable = "something"
if you want to use just in one class you can use :
var yourVariable = "something"

If you don't actually want a global variable, but instead want to save values that persist even when the app closes you can do this:
If you don't actually want to pass data between view controllers but rather simply want to store a global variable you can do this:
This gives a great explanation for how to do this in Swift 5: https://www.hackingwithswift.com/example-code/system/how-to-save-user-settings-using-userdefaults
Summary:
To set a value:
let defaults = UserDefaults.standard
defaults.set("value", forKey: "key")
To get a String value:
let key = defaults.object(forKey: "StringKey") as? [String] ?? [String]()
To get integer value:
let key = defaults.integer(forKey: "IntegerKey")

Related

Whats the difference between initializing a variable within a class VS without??? - swift

Here's my problem, I have a variable that I initialize once so that I don't have to call the database every time I want to use that variable. How should I structure it? Example 1 or 2? What's the difference between putting functions and variables inside a class vs without a class?
example 1 (no class):
import UIKit
var username = ""
func callToDatbase () {
// I run my code to set the username value equal to stored username value within the database
//Ex: username = database.username
}
---------------------------------------------------------------------------------------------------------
//Then when I want to call it in another view controller I would just say:
usernameLabel.text = username
Example 2 (with class):
import UIKit
class Initializers {
var username = ""
func callToDatbase () {
// I run my code to set the username value equal to stored username value within the database
//Ex: self.username = database.username
}
}
---------------------------------------------------------------------------------------------------------
//Then when I want to call it in another view controller I would just say:
usernameLabel.text = Initializers().username
When you initialise a variable outside of the class it is store in the global scope. The scope in swift is defined by the curly brackets, so you can think about the outside of the class as the whole project surrounded by the curly brackets. When you define the variable inside of the class it is only going to be available from that class when you instantiate it as an object. You might want to read about inheritance to find out what else you can do with variables in classes.
TLDR: Variable outside will be available to the whole code, whereas the one inside the class will only be available from within the class or by accessing it as a property from instantiated object.
Example code:
class Example {
var name = "Antoni"
}
print(name) // Will throw error
let example = Example() // Create an instance of the class
print(example.name) // Prints as expected
It is preferred to put variables in some sort of instantiable/destructible structure like a class to help swift with memory management. The variables defined globally will not be removed from memory until the program exits. And if you need to store static variables use struct instead.

Global 'let' declaration requires an initializer expression

I am working on iOS code with Xcode 10.2.1.
I have an issue when declaring a let global variable:
import UIKit
import CouchbaseLiteSwift
private let database: Database
This gives the compiler error below:
Global 'let' declaration requires an initializer expression
I don't know how to make database a global variable in this case. I searched about it, but all says about some int or string initialization but not when initializing classes like Database. Because, Couchbase's Database class is a big class. I can't do it with "= null"
Any help please?
You need a variable if you're not sure what it is so that later you can change it:
private var database: Database?
There's no point in defining a constant if you're not going to actually define it as something.
The distinguishing feature of a let variable is that it is assigned to exactly once before being read.
When a let declaration is a local variable, Swift can analyze the code to make sure that the variable will be initialized exactly once before it is used. This is why you can declare a local let variable with no initial value. For example:
let greeting: String
if(light.isOn()) {
greeting = "Hello!"
} else {
greeting = "Who’s there?!"
}
// Now it is OK to use greeting, because we _know_ the code took
// exactly one branch of the if, and each branch initializes greeting
print(greeting) // OK
With an instance variable, you don't need to specify an initial value as long as every initializer sets it exactly once.
However, with a global variable, the compiler can't really make any guarantees about who will assign to it when. You therefore have to give it an initial value right on the spot.
As #rmaddy points out in the comments, it might look like this:
private let database = Database(name: "foo")
If initialization takes multiple statements, you can batch them up in a closure like this:
private let database = {
let config = readConfigFile()
guard let dbName = config["database"] else {
fatalError("Missing config for 'database'")
}
return Database(name: dbName)
}()
If you must make it a global, but it just isn't possible to initialize it until later, you must make it a var:
private var database: Database?
…or if you want any attempt to use it before it’s initialized to be a crasher:
private var database: Database!

Initializing class constants in Swift

I was trying to do something like this (it is a contrived example for demonstration purposes only):
class Test {
let hello = "hello"
let world = "world"
let phrase: String {
return self.hello + self.world
}
}
but you can't use let for computed properties in Swift. Is there a way to do this without having to write an init() method? Thanks!
The reason let doesn't work on a read-only calculated property is because it's used to state that the property's actual value will never change after being set – not that the property is read-only. As the Apple docs say (emphasis mine):
You must declare computed properties — including read-only computed
properties — as variable properties with the var keyword, because their
value is not fixed. The let keyword is only used for constant
properties, to indicate that their values cannot be changed once they
are set as part of instance initialization.
You therefore need to use var in order to reflect the fact that a calculated property's value could change at any time, as you're creating it on the fly when accessing it. Although in your code, this can't happen – as your hello and world properties are let constants themselves. However, Swift is unable to infer this, so you still have to use var.
For example:
class Test {
let hello = "hello"
let world = "world"
var phrase: String {
return self.hello + self.world
}
}
(This doesn't change the readability of the property – as because you haven't provided it with a setter, it's still read-only)
However in your case, you might want to consider using a lazy property instead, as your hello and world properties are constants. A lazy property is created when it's first accessed, and keeps its value for the rest of its lifetime – meaning you won't have to keep on concatenating two constants together every time you access it.
For example:
class Test {
let hello = "hello"
let world = "world"
lazy var phrase: String = {
return self.hello + self.world
}()
}
Another characteristic of let properties is that their value should always be known before initialisation. Because the value of a lazy property might not be known before then, you also need to define it as a var.
If you're still adamant on wanting a let property for this, then as far as I can see, you have two options.
The first is the neatest (although you've said you don't want to do it) – you can assign your phrase property in the initialiser. As long as you do this before the super.init call, you don't have to deal with optionals. For example:
class Test {
let hello = "hello"
let world = "world"
let phrase: String
init() {
phrase = hello+world
}
}
You simply cannot do it inline, as self at that scope refers to the static class, not an instance of the class. Therefore you cannot access the instance members, and have to use init() or a lazy/calculated property.
The second option is pretty hacky – you can mirror your hello and world properties at class level, so you can therefore access them inline in your phrase declaration. For example:
class Test {
static let hello = "hello"
static let world = "world"
// for some reason, Swift has trouble inferring the type
// of the static mirrored versions of these properties
let hello:String = Test.hello
let world:String = Test.world
let phrase = hello+world
}
If you don't actually need your hello or world properties as instance properties, then you can just make them static – which will solve your problem.
Yes to make it work as computed properties, replace let to var.
Like,
class Test {
let hello = "hello"
let world = "world"
var phrase: String {
return self.hello + self.world
}
}
This way you can use it without init()

Passing variables from one swift file to another

Let's say I have 3 swift files:
fileOne.swift
fileTwo.swift
vars.swift
and let's say that my vars.swift file looks like this:
let let1 = "this is let one"
var isItTrue = false
How can I setup my code in fileOne.swift and fileTwo.swift so that I can access and modify the variables contained within the vars.swift file?
If vars.swift a file from where you will access constants you can create the constants in a struct and then access them from the other file
struct Constants {
static let v1 = "var1"
}
file1.swift
class File1 {
func test() {
print( Constants.v1);
}
}
If your vars.swift file really says let let1=... right at the top level of the file, you can just access let1 from anywhere; all Swift files can see one another, and let1 is a global constant. Similarly for var isItTrue; it is a global variable and any code anywhere can see it and set it.
If what you mean is that vars.swift contains a class declaration with instance variables, then you need to get a reference to an instance in order to set those variables. You need to learn about object-oriented programming (which is what you're doing when you use Swift).
Vars.swift
struct Vars {
static let key1 = "value1"
static let key2 = "value2"
}
Use:
Vars.key1

Global Variable in Swift

I'm creating an iOS 8 app and I want to create a string variable that is accessible to all class. I tried to create global.swift with a string variable in it. I can access the variable but, everytime I initialize the class again using this line let globalVar = global() the value that I've passed returns a nil value. Can someone help me with this. Thank you.
You can put the variable in your AppDelegate, which is a singleton. See this post for how to access the AppDelegate
How do I get a reference to the app delegate in Swift?
Put
public static var global = "String"
in any class. Access via Classname.global.
Just use your Global.swift file and put your global variables and functions inside without any class around it.
//
// Global.swift
//
let myConstant = 5
func myGlobalFunction() {
// ...
}
You can access those variables and functions all around your module.