Swift: how to distinguish a global variable from a class member?

Say you have a named class that you must assign a global variable with the same name.

How to tell the two apart?

var title = "Hello"

class ThisClass {

       var title = ""

       func aMethod () {

            self.title = title
       }
}

      

+3


source to share


1 answer


The scope of the "global" variable title

is a module defined by your Xcode project. By default, its name is the same as your target name. So, if your code is in a project that creates an app called MyApp

, you can do this:



var title = "Hello"

class ThisClass {    
       var title = ""
       func aMethod () {
            self.title = MyApp.title
       }
}

      

+6


source







All Articles