符合协议和类的Swift属性 [英] Swift Property that conforms to a Protocol and Class
问题描述
@property(strong,nonatomic)UIViewController< UITableViewDelegate> *事情;
我想在Swift中实现一个像Objective-C代码中的属性。所以这里是我试过的:
class AClass< T:UIViewController where T:UITableViewDelegate> ;: UIViewController {
var thing:T!
}
编译。我从故事板中添加属性时出现问题。 @IBOutlet
标记会产生编译器错误。
class AClass< T :UIViewController where T:UITableViewDelegate>:UIViewController {
@IBOutlet weak var anotherThing:UILabel! //错误
var thing:T!
$ / code>
错误:
< pre $
$ b $我是否正在执行这项权利?我可以做些什么来解决或解决这个错误?
编辑:
Swift 4终于有了解决这个问题的办法。
Swift 4的更新
Swift 4增加了对将类型表示为符合协议的类的支持。语法是 Class&协议
。下面是一些使用Swift新特性的概念示例代码(WWDC 2017会话402):
$ p $协议Shakeable {
func shake()
}
扩展UIButton:Shakeable {/ * ... * /}
扩展UISlider:Shakeable {/ * ... * /}
//通用示例函数来一般地控制某些控件元素
func shakeEm(控件:[UIControl& Shakeable]){
用于控件的控件,其中control.isEnabled {
控件.shake()
}
}
从Swift 3开始,这种方法会导致问题,因为您无法传入正确的类型。如果你尝试传入 [UIControl]
,它没有 shake
方法。如果您尝试传入 [UIButton]
,那么代码将会编译,但不能传入任何 UISlider
秒。如果你传入 [Shakeable]
,那么你不能检查 control.state
,因为 Shakeable
没有那个。 Swift 4终于解决了这个问题。
旧答案
这个问题暂时用下面的代码: //这个类用来替换UIViewController< UITableViewDelegate>
// Objective-C
中的声明ConformingClass:UIViewController,UITableViewDelegate {}
$ b class AClass:UIViewController {
@IBOutlet weak var anotherThing:UILabel!
var thing:ConformingClass!
}
这对我来说似乎很ha ha。如果需要任何委托方法,那么我将不得不在 ConformingClass
(我不想这样做)中实现这些方法,并在子类中重写它们。 p>
我发布了这个答案,以防其他人遇到此问题并且我的解决方案可以帮助他们,但我对解决方案不满意。如果有人发布了更好的解决方案,我会接受他们的回答。
@property (strong, nonatomic) UIViewController<UITableViewDelegate> *thing;
I want to implement a property like in this Objective-C code in Swift. So here is what I've tried:
class AClass<T: UIViewController where T: UITableViewDelegate>: UIViewController {
var thing: T!
}
This compiles. My problem comes when I add properties from the storyboard. The @IBOutlet
tag generates an compiler error.
class AClass<T: UIViewController where T: UITableViewDelegate>: UIViewController {
@IBOutlet weak var anotherThing: UILabel! // error
var thing: T!
}
The error:
Variable in a generic class cannot be represented in Objective-C
Am I implementing this right? What can I do to fix or get around this error?
EDIT:
Swift 4 finally has a solution for this problem. See my updated answer.
Update for Swift 4
Swift 4 has added support for representing a type as a class that conforms to a protocol. The syntax is Class & Protocol
. Here is some example code using this concept from "What's New in Swift" (session 402 from WWDC 2017):
protocol Shakeable {
func shake()
}
extension UIButton: Shakeable { /* ... */ }
extension UISlider: Shakeable { /* ... */ }
// Example function to generically shake some control elements
func shakeEm(controls: [UIControl & Shakeable]) {
for control in controls where control.isEnabled {
control.shake()
}
}
As of Swift 3, this method causes problems because you can't pass in the correct types. If you try to pass in [UIControl]
, it doesn't have the shake
method. If you try to pass in [UIButton]
, then the code compiles, but you can't pass in any UISlider
s. If you pass in [Shakeable]
, then you can't check control.state
, because Shakeable
doesn't have that. Swift 4 finally addressed the topic.
Old Answer
I am getting around this problem for the time being with the following code:
// This class is used to replace the UIViewController<UITableViewDelegate>
// declaration in Objective-C
class ConformingClass: UIViewController, UITableViewDelegate {}
class AClass: UIViewController {
@IBOutlet weak var anotherThing: UILabel!
var thing: ConformingClass!
}
This seems hackish to me. If any of the delegate methods were required, then I would have to implement those methods in ConformingClass
(which I do NOT want to do) and override them in a subclass.
I have posted this answer in case anyone else comes across this problem and my solution helps them, but I am not happy with the solution. If anyone posts a better solution, I will accept their answer.
这篇关于符合协议和类的Swift属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!