我正在尝试在 Objective-C 类中使用 Swift,但语法有问题,或者即使有可能。下面是我正在尝试的。
迅速:
@objc protocol MyProtocol {
var someManager: MyManager? { get set }
var someState: MyState { get set }
}
extension MyProtocol {
func didLoad(delegate: MyProtocol) {
}
}
class ViewController: MyProtocol {
var someManager: MyManager?
var someState: MyState = .Unknown
override func viewDidLoad() {
super.viewDidLoad()
didLoad(self as MyProtocol)
}
}
目标-C:
@interface ViewController : UIViewController<MyProtocol>
@end
@interface ViewController()
@property (nonatomic, assign) MyManager *someManager; //?? Error: Illegal redeclaration of property in class extension ‘ViewController’ (attribute must be ‘readwrite’, while its primary must be ‘readonly’)
@property (nonatomic, assign) MyState someState; //??
@end
@implementation HomeViewController
-(void)viewDidLoad {
[super viewDidLoad];
[self didLoad:(self as MyProtocol)] //??
}
如何让 Objective-C ViewController 工作?我有一个例子说明它应该如何在 Swift 中工作。谢谢你的帮助。