2017-06-20 57 views
1

我正在使用委託來傳遞我存儲在函數中的值。每當我嘗試實現委託到我的另一個類,我得到錯誤「AnswerViewController」不符合協議「TagToIndex委託」。擴展,錯誤產生:「協議需要功能與類型」是什麼意思?

協議需要函數'finishPassing(dictionary :)'與類型'(Dictionary) - >()'你想添加一個存根嗎?

這是協議:

protocol TagToIndexDelegate { 
func finishPassing (dictionary:Dictionary<Int,Int>) 
} 

這裏是我想從發送變量的函數:

extension MyCell: YSSegmentedControlDelegate { 

func segmentedControl(_ segmentedControl: YSSegmentedControl, willPressItemAt index: Int) { 
    tagToIndex[actionButton.tag] = index 

    delegate?.finishPassing(dictionary: tagToIndex) 
} 

func segmentedControl(_ segmentedControl: YSSegmentedControl, didPressItemAt index: Int) { 

}} 

哪裏delegateTagToIndexDelegate型的,和變量tagToIndex這存在於willPressItemAt之內是我傳遞的數據。

最後,類我想實現TagToIndexDelegate

class AnswerViewController: UIViewController, TagToIndexDelegate { 
override func viewDidLoad() { 
    super.viewDidLoad() 

} 
} 

我覺得我已經做了某種根本性的錯誤,但我沒有足夠的熟悉斯威夫特知道是什麼錯誤。

謝謝, 尼克

回答

1

您已定義協議TagToIndexDelegate這需要實現的方法finishPassing (dictionary:Dictionary<Int,Int>)。然後,你說你的AnswerViewController類符合TagToIndexDelegate,但你永遠不會實際實現所需的方法。該類需要實施所需的方法以滿足一致性。

的錯誤暗示這將是您可以添加一個存根:

optional func finishPassing (dictionary:Dictionary<Int,Int>)

func finishPassing (dictionary:Dictionary<Int,Int>) 
{ 
    // logic here 
} 

您還可以通過在它前面加上optional這樣的更改協議函數聲明可選

至於什麼是正確的做法,您必須根據應用程序中實際發生的情況來決定。

相關問題