4

I need some advice on the global declaration of variables (and hopefully functions)

What is the difference between declaring a variable like

import Foundation

var myglobalvariable = ""

and

import Foundation

struct globalvariablestruct
{
static var myglobalvariable = ""
}

and

import Foundation

class globalstructoperation {
      struct glovalVariable {
      static var myglobalvariable = ""
     }
 }

Also, I have an API, that is used around 5 times in different view controllers, would it be ok to declare it as a global function?

import Foundation
import Alamofire

func myapicall()
{
//stuff
}

Will Apple reject an app if there are (a lot of) global variables/functions?

What would be the best way to pass variables between several ViewControllers? Also, there are some variables which are used on 90% of the ViewControllers, is it OK if I declare those are global variables?

Zyfe3r
  • 653
  • 6
  • 24
  • 1
    “What is the difference” none except for namespacing. – matt Jun 17 '19 at 05:45
  • 1
    Apple don't care even if you declare all variables global. You can go through this [question](https://stackoverflow.com/questions/10525582/why-are-global-variables-considered-bad-practice) to find reasons why you should avoid global variables. – Kamran Jun 17 '19 at 05:49

2 Answers2

3

There are lots of questions in this question. I have tried to answer some of the questions. Maybe it will help you.

Approach 1

var myglobalvariable = ""

You can access these types of variables without any reference. You can define it in any file and can access it in the 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.

Approach 2

struct globalvariablestruct{
    static var myglobalvariable = ""
}

You can access these types of variables with struct name or you can say that we need to use struct name to access these varibles.

Approach 3

class globalstructoperation {
      struct glovalVariable {
         static var myglobalvariable = ""
     }
 }

You can access these types of variables with struct and class name. Also, It creates a pass by reference variable using a struct.

Also, I have an API, that is used around 5 times in different view controllers, would it be ok to declare it as a global function?

Yes, You can go with a global function or create a singleton class for the same.

Will Apple reject an app if there are (a lot of) global variables/functions?

No, because of Apple reviewer team only checked the functionality of our app and app do not violate any Apple policy.

What would be the best way to pass variables between several ViewControllers? Also, there are some variables which are used on 90% of the ViewcControllers, is it OK if I declare those are global variables?

Yes, You can define the n number of global variables in the project because Apple doesn't care about the internal development methodology.

Hitesh Surani
  • 12,733
  • 6
  • 54
  • 65
1

This is all about one of the object-oriented programming concepts. And here is the question is what is encapsulation?

let pi = 3.14159

The first approach is generally terrible form. If you just have a few variables in your whole project, it can be okay, but what happens when there are many many? Let's think about your project team includes 10 ios developers. What will happen when someone wants to use this global variable. How can he/she find them and understand what they are?

class MathObjects{
   static let pi = 3.14159
}
print(MathObjects.pi)

I think, the better way is to encapsulate your global variables into their own class. This keeps everything all in one place and gives you the added benefit of XCodes’ handy autocomplete.

KingHodor
  • 537
  • 4
  • 17
  • So whats the difference/advantage between encapsulating them inside a struct VS class? – Zyfe3r Jun 17 '19 at 05:53
  • 1
    If you only use the static variables, there is no advantage or disadvantage in here. But generally why Choose Struct Over Class? Here is the answer : https://stackoverflow.com/questions/24232799/why-choose-struct-over-class – KingHodor Jun 17 '19 at 05:59