2015-06-19 29 views
0

我是Swift的新手,我试图访问一个'problemSolved'数组,该数组在另一个类的主GameController类的游戏中添加。出于某种原因,数组在UIViewController类中不可见,我想在其中显示表中解决的所有问题。我已经阅读了网站上的许多Singleton示例,看看这是否会实现,但似乎并不如此。任何帮助或建议,在这里非常感谢!swift中的单例不能从另一个类访问

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

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

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

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)") 

} 
+2

变量“问题”来自哪个附加数组? – Qbyte

+0

嗨Qbyte,游戏是一个单词匹配游戏。在GameController中有一个func dealWord(),它从Level类中的'words'数组中抽取一个单词。 (我似乎可以从GameController中访问,但不是GameController,因此这个问题!)。一旦函数dealWord()完成func onProblemSolved,它将检查问题是否用if语句解决,如果是,则将该单词添加到problemSolved数组中。思考? – richc

回答

0

此刻,我只能想象,你不叫

GameController.sharedInstance.onProblemSolved() 

当你想为一个字符串追加到problemsSolved

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

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

+0

辉煌Qbyte,这确实是我的错误。非常感谢! – richc

+0

不客气!你能将答案标记为“正确”吗?所以其他人可以看到你的问题解决了。 – Qbyte

相关问题