In Objective-C I used to have external variables using this statement:
extern int GlobalInt;
And it can be accessed and changed from any class without any restrictions.
How can I produce such a variable in Swift 3.0?
According to the answers it is very difficult to know the class that variable is declared on. Making everything mess up.
Is there any workaround to increase to make code clear and understandable for future modifications?
How do they replaced atomicity, access, storage property attributes from objective-C.
Do they removed thus features or can we achieve these in swift?
5 Answers
Answers 1
Use:
var globalInt = 0
Explanation
Extern is a construct in (Objective-)C to tell the compiler, that the variable is defined somewhere else (the data is somewhere else). Where is then resolved by the linker (last stage when creating an executable file).
You typically put extern int GlobalInt
in .h
file and then int GlobalInt
in .m
file.
In Swift you don't need that, because you don't have a separate header files. Just create a global variable.
If you are building a framework, you might need to add access modifier, like public
to your definition.
Answers 2
What you need to do is just basically declare variable outside of any class scope...
var globalVar = 0 class MyClass{ //some declarations... }
For better project structure, you can make some file containing global variables, like obviously Globals.swift
and these variables store in there.
In there, you can set any properties you want, don't make any class, struct or whatever. This will serve you well as the global variables will be available throught your whole application. Problem with this approach is that it becomes messy and when you want to declare some variable inside class which by an accident has same identifier than the global one, you gonna have really bad time. For this cases, it is better to create some singleton object, which holds all the values for you. I would prefer singletons over global variables, since you know what are you looking for...
Generally, it is just better to avoid global variables as much as possible to avoid conflicts.
Answers 3
For more Separation, You may add a new file for External variables only like
import Foundation public class GlobalVariable{ public static var gv_UserId:Int? public static var gv_UserName:String = "welcome" public static var gv_Password:String = "welcome" }
Answers 4
Make int variable in your AppDelegate Like
var globalInt = Int()
and where you are getting value of int send its value to AppDelegate. make AppDelgate object like below
let appDelegate = UIApplication.shared.delegate as! AppDelegate
to pass value to app delegate variable
self.appDelegate. globalInt = "your Value"
and get your values wherever you want to fetch like below
let value = appDelegate. globalInt as! Int
values passed
and Update value anywhere you want by declaring app delegate object and writing following line
self.appDelegate. globalInt = "your Value"
Answers 5
let string = MyVariables.yourVariable println("Global variable:\(string)")
Changing value of variable:
MyVariables.yourVariable = "anotherString"
0 comments:
Post a Comment