如题, 比如
@protocol A <NSObject>
@end
@interface B : UIView
@property UIView C<A> *c;
@end
这个代码用swift怎么实现
protocol A {
}
class B: UIView {
//var c: C, A
//var c: protocol<C,A>
// 上面两种都不行
}
在线等...
如题, 比如
@protocol A <NSObject>
@end
@interface B : UIView
@property UIView C<A> *c;
@end
这个代码用swift怎么实现
protocol A {
}
class B: UIView {
//var c: C, A
//var c: protocol<C,A>
// 上面两种都不行
}
在线等...
import UIKit
class ViewController: UIViewController {
var po:(UIView&TestPol)!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
po = TestCl()
}
}
protocol TestPol {
}
class TestCl:UIView, TestPol {
}
2 回答615 阅读
539 阅读
在 Swift 中,如果是原生类型(如:Int、String)的话,通过
extension
让原生类型遵守新 Protocol。如果是自定义类型,就直接遵守即可。
具体示例代码如下: