0

how do I import a swift file in another swift file so I could use variables. Here is my code from menuViewController.swift and I want to use variable selectedObject

import UIKit

    class menuViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    var selectedObject: String!
}

I want to use the variable from above file in ViewController.swift. Here is my ViewController.sswift

import UIKit

class ViewController: UIViewController, UIAlertViewDelegate {

}                        

2 Answers 2

2

If they are in the same target you do not need to import other Swift files.

import UIKit

class MenuViewController: UIViewController {

    var selectedObject: String!
}


class ViewController: UIViewController {
    let menuViewController = MenuViewController()


    func doSomething() {
        menuViewController.selectedObject = "foo"
    }
}

The above is working fine for me.

Sign up to request clarification or add additional context in comments.

4 Comments

what do you mean by same target? ... I have only one target, so how should I check if it's the same
If you have only one, then you should be fine. You can check Target Membership in the Utilities pane. i.imgur.com/hXAxqu4.png
I have all swift files listed in Target's Compile sources section.. Take a look here postimg.org/image/xlr9h77bj
Can you post to imgur or dropbox? That domain is blocked at my office
0

All you need todo is import the project. So say if your project is called my project, do:

import myproject

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.