无法从另一个 class 访问 swift 中的单例

Singleton in swift is not accessible from another class

我是 Swift 的新手,我正在尝试从另一个 class 访问我的主 GameController class 游戏期间附加的 'problemSolved' 数组。由于某种原因,该数组在 UIViewController class 中不可见,我想在其中显示在 table 中解决的所有问题。我已经阅读了该站点上的许多 Singleton 示例,以查看是否可以做到这一点,但似乎并没有。非常感谢这里的任何帮助或建议!

class GameController: TileDragDelegateProtocol, CenterViewControllerDelegate {
   static let sharedInstance = GameController()
   var problemsSolved = Array<String>()  

   func onProblemSolved() {
     problemsSolved.append(problem)
     println("problemsSolved contains \(problemsSolved)")
}
}

在游戏过程中,我可以在控制台中看到数组正在 GameController 中正常附加。但是当我尝试在 ViewController class 中访问它时,内容显示为空 [].

class SidePanelViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

@IBOutlet weak var tableView: UITableView!

override func viewDidLoad() {
tableView.reloadData()
println("the array viewed from here is empty \(GameController.sharedInstance.problemsSolved)")

}

此刻我只能想象你不打电话

GameController.sharedInstance.onProblemSolved()

当您想将字符串附加到 problemsSolved 时。

您还应该考虑将 GameController 中的函数和变量设为静态。

如果这不能解决您的问题,我需要更多有关如何以及何时向 problemsSolved 添加内容的信息。