Swifte - Xcode协议混淆

时间:2015-06-12 15:09:16

标签: ios xcode swift

我开始使用协议但遇到了一些麻烦。

我制定了我的协议:

protocol DataSource: class {
    func getSpeedFromViewController(sender: GameScene) -> Float
    func addPoints(pointsToAdd: Int, sender: GameScene)
    func removePoints(pointsToRemove: Int, sender: GameScene)
}

在我的Gamescene:

// protocol
weak var dataSource: DataSource?

在我的GameViewController中:

// protocol conformation
scene.dataSource = self

// first one, getspeed
func getSpeedFromViewController(sender: GameScene) -> Float{
    return Slider.value
}

// add Points
func addPoints(pointsToAdd: Int, sender: GameScene){
    Man.addPoints(pointsToAdd)
}

//remove Points
func removePoints(pointsToRemove: Int, sender: GameScene){
    Man.removePoints(pointsToRemove)

}

现在getSpeedFromViewController工作得非常完美,我在场景中调用它,获得速度,一切都很棒。

但是我无法让其他功能发挥作用。我尝试了很多东西,它只是不起作用。

如果我尝试使用它们:

DataSource.addPoints(10,sender: self)

我在通话中收到错误“额外参数sender。 如果我删除“sender:”,我会在调用中获得额外的参数。 如果我删除所有内容,只需执行

addPoints(self)

我得到“无法使用addPoints

类型的参数调用(GameScene)

如果我删除了Int传递他们的工作。所以问题是它不会让我通过协议传递其他任何东西。我做错了什么?

1 个答案:

答案 0 :(得分:1)

我不能相信它,我迟迟不敢。所以问题是我正在使用" DataSource"这是链接到协议。我必须使用" dataSource"链接到var。我没有意识到我使用的是错误的,因为我把它们命名得那么糟糕。无论如何感谢帮助人员。

相关问题