Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 通用参数类型';T';无法推断_Ios_Swift_Generics - Fatal编程技术网

Ios 通用参数类型';T';无法推断

Ios 通用参数类型';T';无法推断,ios,swift,generics,Ios,Swift,Generics,我从代码“无法推断泛型参数类型”中得到一个错误 public func userTappedView(headerFooterView: CollapsableTableViewSectionHeaderProtocol, atPoint location:CGPoint) { if let tableView = self.collapsableTableView() { //Error here let value = se

我从代码“无法推断泛型参数类型”中得到一个错误

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? {
    ...
}
public func userTappedView(headerFooterView:collapsabletableviewssectionheaderprotocol,atPoint位置:CGPoint){
如果let tableView=self.collapsableTableView(){
//这里出错
让value=sectionForUserSelectionInTableView(tableView,atTouchLocation:location,inView:headerFooterView)
...
}
...
}
用户选择tableView(tableView:UITableView,atTouchLocation位置:CGPoint,inView视图:T)的func部分->NSNumber?{
...
}

我希望我能理解您的问题,即确保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
    }
}


我希望我能理解您的问题,即确保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
    }
}


您正在将CollapsableTableViewSectionHeaderProtocol传递给需要UITableViewHeaderFooterView的方法。任何类都可以实现该协议;因此,编译器无法推断您传递的对象是UITableViewHeaderFooterView子类。如何确保UITableViewHeaderFooterView的子类符合协议CollapsableTableViewSectionHeaderProtocol?如果您知道它将是UITableViewHeaderFooterView,则可以强制转换它,或者将userTappedView的定义更改为需要UITableViewHeaderFooterView。必须有其他方法来处理此问题。对于类类型,编译器可以通过查看其定义来确定是否符合协议。另一方面,协议类型可以是(字面上)符合该协议的任何类,因此编译器无法确定它是否是特定类的子类。您正在将CollapsableTableViewSectionHeaderProtocol传递给需要UITableViewHeaderFooterView的方法。任何类都可以实现该协议;因此,编译器无法推断您传递的对象是UITableViewHeaderFooterView子类。如何确保UITableViewHeaderFooterView的子类符合协议CollapsableTableViewSectionHeaderProtocol?如果您知道它将是UITableViewHeaderFooterView,则可以强制转换它,或者将userTappedView的定义更改为需要UITableViewHeaderFooterView。必须有其他方法来处理此问题。对于类类型,编译器可以通过查看其定义来确定是否符合协议。另一方面,协议类型可以是(字面上)符合该协议的任何类,因此编译器无法确定它是否是特定类的子类。