我在协议中有两种方法
func gameBoard(gameBoard : HSGameBoardViewController, canMovePieceFrom startIndex: NSIndexPath, to endIndex: NSIndexPath) -> Bool!
func gameBoard(gameBoard : HSGameBoardViewController, checkIfCellAtIndex startIndexPath: NSIndexPath, hasTheSameOwnerAsCellAt endIndexPath: NSIndexPath) -> Bool!
两者都使用相同的三个参数并返回相同的类型但是具有不同的外部标识符。
在实施协议时,Xcode提出了这个建议:
Method 'gameBoard(_:canMovePieceFrom:to:)' has different argument names from those required by protocol 'HSGameBoardViewControllerDelegate' ('gameBoard(_:checkIfCellAtIndex:hasTheSameOwnerAsCellAt:)')
所以我不确定为什么我不能这样做?
我已经检查了UICollectionViewDelegate
,他们有方法可以做我正在尝试做的事情。例如:
func collectionView(_ collectionView: UICollectionView, shouldSelectItemAtIndexPath indexPath: NSIndexPath) -> Bool
func collectionView(_ collectionView: UICollectionView shouldDeselectItemAtIndexPath indexPath: NSIndexPath) -> Bool
答案 0 :(得分:1)
您应该实现两种协议方法:
class HSGameBoardViewController{
}
protocol HSGameBoardViewControllerDelegate {
func gameBoard(gameBoard : HSGameBoardViewController, canMovePieceFrom startIndex: NSIndexPath, to endIndex: NSIndexPath) -> Bool!
func gameBoard(gameBoard : HSGameBoardViewController, checkIfCellAtIndex startIndexPath: NSIndexPath, hasTheSameOwnerAsCellAt endIndexPath: NSIndexPath) -> Bool!
}
class Delegate: HSGameBoardViewControllerDelegate {
func gameBoard(gameBoard: HSGameBoardViewController, canMovePieceFrom startIndex: NSIndexPath, to endIndex: NSIndexPath) -> Bool! {
return false
}
func gameBoard(gameBoard : HSGameBoardViewController, checkIfCellAtIndex startIndexPath: NSIndexPath, hasTheSameOwnerAsCellAt endIndexPath: NSIndexPath) -> Bool! {
return false
}
}
仅实现第一种方法会出现此错误:
Method 'gameBoard(_:canMovePieceFrom:to:)' has different argument names from those required by protocol 'HSGameBoardViewControllerDelegate' ('gameBoard(_:checkIfCellAtIndex:hasTheSameOwnerAsCellAt:)')
但真正的错误是你没有完全实现协议。
您可以在操场上粘贴此示例,并且会看到没有编译器错误。