我正在构建一个基于旧Obj-c应用程序的新Swift应用程序。 我目前正在处理代理。
以下是我的.obj-c代码在.h文件中的样子:
@interface MyAppViewController : CustomViewController
@property (nonatomic, weak) id<MyAppViewControllerDelegate> delegate;
@end
@protocol MyAppViewControllerDelegate <NSObject>
- (void)myAppViewController:(MyAppViewController *)controller loggedInStudent: (MYStudent *)student;
- (void)myAppViewControllerWantsSignUp:(MyAppViewController *)controller;
@end
在 SWIFT 中,我做了以下工作:
class MyAppViewController: CustomViewController {
var delegate: MyAppViewControllerDelegate?
protocol MyAppViewControllerDelegate{
func myAppViewController(controller: MyAppViewController, loggedInStudent: MYStudent)
func myAppViewControllerWantsSignUp(controller: MyAppViewController)
我已经做了很多阅读和研究,所以我认为我基本上是正确的(虽然对swift完全不熟悉...)。但是我遇到了这个错误,“
Declaration is only valid in file scope
”,出现在 protocol MyAppViewControllerDelegate {
上。我认为这与在类内部声明有关,因此我将其移出,但现在我在类内部的代码中无法识别我声明的委托变量... 有什么想法吗?