swift - 如何将 swift 文件导入另一个文件

标签 swift

如何在另一个 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/

相关文章:

swift - 滚动后在 Collection View 单元格上随机显示事件指示器

swift - 使用 Swift Parse Server 和 AWS SES 请求重置电子邮件密码

swift - 复制/重现 UIAlertView 模糊效果

ios - Firebase 填充数组两次 -- Swift

ios - 如何正确关闭模态呈现的 Controller

swift - UICollectionView 的标题 View 在滚动时不断重复 - 如何创建固定(非粘性)的单个 Collection View 标题?

ios - 函数中具有相互递归关联类型约束的 Swift 协议(protocol)

ios - UI 元素未显示它们在 xcode 中 Storyboard 上的实际位置

arrays - 无法调用类型 : with an argument list of type: 的初始值设定项

ios - 如何通过 Observable 属性对项目数组进行排序?