如何使Swift协议符合特定的Class类? [英] How to make Swift protocol conformed by a specific kind of Class?

查看:120
本文介绍了如何使Swift协议符合特定的Class类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Swift中写了一个协议:

  Protocol FooDelegate {
titleImage:UIImage? {get}
}

我要确保符合该类的类必须就是UITableViewController类型。



换句话说:

  // ViewController符合FooDelegate可以,因为ViewController 
//从UITableViewController继承!
类ViewController:UITableViewController,FooDelegate / *编译正常* / {

}

// OtherVC无法符合FooDelegate,因为它不是继承
//来自UITableViewController。
类OtherVC:UIViewController,FooDelegate / *将编译错误* / {

}

该怎么做?



我将FooDelegate的定义更改为:

 协议FooDelegate其中Self:UITableViewController {
var titleImage:UIImage? {get}
}

但这似乎不起作用。



*固定部分*
使用Self:UITableViewController可以!!!!



但是当我在下面写时,编译时是错误的:

  class StubClass {
var委托:FooDelegate!

func invoke(){
let idxSet = ...
代表没有成员'tableView'
}
}

请查看以下行上面那个错误。委托中没有tableView属性吗?委托符合FooDelegate,并且FooDelegate设置Self:UITableViewController和UITableViewController必须具有tableView属性?? !!



这里出了什么问题???谢谢:)

解决方案

由于 tableView 属性对协议不可见声明,您只需要在协议中添加 tableView 属性。像这样的东西: $ b}

类SimpleTableView:UITableViewController,FooDelegate {

}

让委托:FooDelegate吗? = SimpleTableView()

代表?.tableView.reloadData()

记住在协议声明中添加 @objc


I wrote a protocol in Swift:

protocol FooDelegate{
    titleImage:UIImage? {get}
}

I want to make sure that the class that conforms to it must be a UITableViewController type.

In other words:

// ViewController conform FooDelegate is OK,Because ViewController 
// is inherit from UITableViewController!
class ViewController:UITableViewController,FooDelegate /* compile ok */{

}

// OtherVC is failed to conform FooDelegate,Because it is not inherit
// from UITableViewController.
class OtherVC:UIViewController,FooDelegate /* will compile error */{

}

How to do that?

I changed FooDelegate's definition to this:

protocol FooDelegate where Self:UITableViewController {
    var titleImage:UIImage? {get}
}

But it does not seem to work.

* FIX PART *: Use Self:UITableViewController is OK!!!

But when I write below is wrong when compiling :

class StubClass{
    var delegate:FooDelegate!

    func invoke(){
        let idxSet = ...
        delegate.tableView.reloadSections(idxSet, with: .none) //Error:Value of type 'FooDelegate' has no member 'tableView'
    }
}

Please look at the line that Error above. Isn't there any tableView property in delegate? delegate is conform to FooDelegate,and FooDelegate make Self:UITableViewController,and UITableViewController must have tableView property??!!

What's wrong here??? Thanks :)

解决方案

As tableView property is not visible to the protocol declaration you just need to add the tableView property in the protocol. Something like this:

@objc protocol FooDelegate where Self: UITableViewController {
    var tableView: UITableView { get }
}

class SimpleTableView: UITableViewController, FooDelegate {

}

let delegate: FooDelegate? = SimpleTableView()

delegate?.tableView.reloadData()

Remeber to add @objc in protocol declaration.

这篇关于如何使Swift协议符合特定的Class类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆