如何在另一个 swift 文件中导入一个 swift 文件,以便我可以使用变量。这是我来自 menuViewController.swift 的代码,我想使用变量 selectedObject
import UIKit
class menuViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
var selectedObject: String!
}
我想在 ViewController.swift 中使用上述文件中的变量。这是我的 ViewController.sswift
import UIKit
class ViewController: UIViewController, UIAlertViewDelegate {
}
最佳答案
如果它们在同一目标中,则无需导入其他 Swift 文件。
import UIKit
class MenuViewController: UIViewController {
var selectedObject: String!
}
class ViewController: UIViewController {
let menuViewController = MenuViewController()
func doSomething() {
menuViewController.selectedObject = "foo"
}
}
以上对我来说工作正常。
关于swift - 如何将 swift 文件导入另一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25248110/