swift中的Singleton无法从其他类访问
我是Swift新手,正在尝试从另一个类访问一个“problemSolved”数组,该数组在游戏过程中附加在我的主GameController类中。由于某些原因,数组在UIViewController类中不可见,我想在表中显示已解决的所有问题。我已经阅读了网站上的许多单例示例,看看这样做是否可行,但似乎不行。非常感谢这里的任何帮助或建议swift中的Singleton无法从其他类访问,swift,singleton,Swift,Singleton,我是Swift新手,正在尝试从另一个类访问一个“problemSolved”数组,该数组在游戏过程中附加在我的主GameController类中。由于某些原因,数组在UIViewController类中不可见,我想在表中显示已解决的所有问题。我已经阅读了网站上的许多单例示例,看看这样做是否可行,但似乎不行。非常感谢这里的任何帮助或建议 class GameController: TileDragDelegateProtocol, CenterViewControllerDelegate {
class GameController: TileDragDelegateProtocol, CenterViewControllerDelegate {
static let sharedInstance = GameController()
var problemsSolved = Array<String>()
func onProblemSolved() {
problemsSolved.append(problem)
println("problemsSolved contains \(problemsSolved)")
}
}
现在我只能想象你不打电话
GameController.sharedInstance.onProblemSolved()
当您要将字符串附加到已解决的问题时
您还应该考虑在<代码> GameController < /COD> static .</P>中编写函数和变量。
如果这不能解决您的问题,我需要更多关于如何以及何时向
problemsSolved
添加内容的信息。您添加到数组的变量“problem”来自何处?您好,Qbyte,游戏是一个单词匹配游戏。在GameController中有一个func dealWord(),它从Level类中的“words”数组中提取一个单词。(我似乎可以从GameController访问,但不是太GameController,因此出现了这个问题!)。func dealWord()完成后,将出现func onProblemSolved,用于检查问题是否已通过if else语句解决,如果已解决,则将单词添加到problemSolved数组中。想法?聪明的Qbyte,那确实是我的错误。非常感谢!不客气!你能把答案标为“对”吗?这样其他人就可以认为你的问题已经解决了。
GameController.sharedInstance.onProblemSolved()