我正在尝试在GameViewController中发生事件时在View Controller中执行某些操作。
所以这就是我的工作
在GameViewController中
typealias CallbackBlock = () -> Void
public class GameController {
var onAnagramSolved : CallbackBlock!
func inSomeMethod(){
self.onAnagramSolved()
}
}
在ViewController中
public class ViewController: UIViewController {
var gameController: GameController!
public override func loadView() {
super.loadView()
gameController = GameController()
}
override public func viewDidLoad() {
super.viewDidLoad()
// This is where I get the error (which is the title)
gameController.onAnagramSolved = self.showLevelMenu()
}
func showLevelMenu(){
// A method showing UIAlertController
}
然后我得到错误说
Cannot assign a value of type '()' to type 'CallbackBlock!'
它在Swift 3中。
答案 0 :(得分:1)
尝试删除方法名称后面的()
。也就是说,改变这一行:
gameController.onAnagramSolved = self.showLevelMenu()
到此:
gameController.onAnagramSolved = self.showLevelMenu
第一种语法用于函数调用,另一种用于函数引用(即产生对给定函数的引用)。
更明确的选择是使用闭包:
gameController.onAnagramSolved = { self.showLevelMenu() }