Search code examples
iosswiftglobal-variables

Problem accessing variable declared in one file in extension file


I declared a variable in my ViewController.swift file like this:

struct serials{
    static var snToConnectTo2 = ""
}

class ViewController: UIViewController {
....
}

I want to access this variable from my KeyboardViewController.swift file which belongs to an extension of my application that ViewController.swift controls.

I am trying to access the variable like this:

serials.snToConnectTo2

When I try to compile my code I get the following error:

Use of unresolved identifier 'serials';

How do you properly access variables from different files?


Solution

  • first of all you need to declare your struct as public and its properties like the following:

     public struct serials{
     public static var snToConnectTo2 = ""
    
     public init() {
    
     }
    }
    

    second you need to link this file (I mean the file that contain your struct) as described in the following screenshot

    enter image description here

    then choose your file and add it, and it will work fine with you