2016-01-31 2 views
1

Я получаю сообщение об ошибке из этого кода. Тип универсального параметра «T» не может быть выведен ».Общий параметр типа 'T' не может быть выведен.

public func userTappedView(headerFooterView: CollapsableTableViewSectionHeaderProtocol, atPoint location:CGPoint) { 

     if let tableView = self.collapsableTableView() { 

      //Error here 
      let value = sectionForUserSelectionInTableView(tableView, atTouchLocation: location, inView: headerFooterView) 
      ... 
     } 
    ... 
} 


func sectionForUserSelectionInTableView<T: UITableViewHeaderFooterView where T: CollapsableTableViewSectionHeaderProtocol>(tableView: UITableView, atTouchLocation location:CGPoint, inView view: T) -> NSNumber? { 
    ... 
} 
+0

вы передаете CollapsableTableViewSectionHeaderProtocol к способу, который требует UITableViewHeaderFooterView. Любой класс может реализовать этот протокол; поэтому компилятор не может сделать вывод о том, что объект, который вы передаете, является подклассом UITableViewHeaderFooterView. – titaniumdecoy

+0

Как обеспечить соответствие подкласса UITableViewHeaderFooterView протоколу CollapsableTableViewSectionHeaderProtocol? –

+0

Вы можете сделать это, если знаете, что это будет UITableViewHeaderFooterView или изменить определение userTappedView, чтобы потребовалось UITableViewHeaderFooterView. – titaniumdecoy

ответ

0

Я надеюсь, что я понимаю ваш вопрос, который должен обеспечить подкласс UITableViewHeaderFooterView соответствует CollapsableTableViewSectionHeaderProtocol протокола.

Если мое предположение верно, то вы можете сделать что-то вроде этого:



//// Define a protocol 
protocol CollapsableTableViewSectionHeaderProtocol { 
    func sectionForUserSelectionInTableView(tableView: UITableView, atTouchLocation location:CGPoint, inView view: T) -> NSNumber? 
} 

class MyTableViewController: UITableViewController { 
    internal func userTappedView(headerFooterView: UITableViewHeaderFooterView, atPoint location:CGPoint) { 
     //// Call CollapsableTableViewSectionHeaderProtocol method 
     let value = headerFooterView.sectionForUserSelectionInTableView(self.tableView, atTouchLocation: location, inView: headerFooterView) 
     print(value) 
    } 
} 

//// Implement CollapsableTableViewSectionHeaderProtocol for UITableViewHeaderFooterView via extension 
extension UITableViewHeaderFooterView: CollapsableTableViewSectionHeaderProtocol { 
    func sectionForUserSelectionInTableView(tableView: UITableView, atTouchLocation location:CGPoint, inView view: T) -> NSNumber? { 
      //.... 
      return nil 
    } 
} 

Смежные вопросы