File size? Quick delegates and protocols

I am working on a new Swift application based on the old Obj-c application. I am currently working on delegates

This is what my obj-c code in the .h file looked like

@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 

In SWIFT, I did:

 class MyAppViewController: CustomViewController { var delegate: MyAppViewControllerDelegate? protocol MyAppViewControllerDelegate{ func myAppViewController(controller: MyAppViewController, loggedInStudent: MYStudent) func myAppViewControllerWantsSignUp(controller: MyAppViewController) 

I read and studied this a lot, so I thought that I was doing it mostly correctly (completely new for fast, though ... like that)

I get this error though, Declaration is only valid in file scope "on protocol MyAppViewControllerDelegate { I assumed that it had something to do with declaring it inside the class, so I moved it, only now my code inside the class does not recognize the delegate variable, which I announced.

Any ideas?

+6
source share
2 answers

Must be:

 protocol MyAppViewControllerDelegate { func myAppViewController(controller: MyAppViewController, loggedInStudent: MYStudent) func myAppViewControllerWantsSignUp(controller: MyAppViewController) } class MyAppViewController: CustomViewController { var delegate: MyAppViewControllerDelegate? } 

Although, if you follow the general pattern, where the object that MyAppViewController belongs to is also its delegate, it can cause memory problems. You can use class for input for weak delegation:

 protocol MyAppViewControllerDelegate : class { func myAppViewController(controller: MyAppViewController, loggedInStudent: MYStudent) func myAppViewControllerWantsSignUp(controller: MyAppViewController) } class MyAppViewController: CustomViewController { weak var delegate: MyAppViewControllerDelegate? } 

This is a bit limiting, because it requires using a class for your delegate, but this will help avoid saving loops :)

+6
source

From what I see in your source code, is that you declared your protocol inside your class. Just declare the protocol outside the class declaration and everything will be fine.

Update:

The default access level is set to internal, which is defined as

Internal access allows an entity to be used in any source file from its defining module,> but not in any source file outside this module. Usually you use internal access when> you define applications or the internal structure of the framework.

Unlike Objective-C or C, you do not need a forward declaration if the implementation was not executed before use.

Source: Fast Programming Language, Access Control

+2
source

Source: https://habr.com/ru/post/976737/


All Articles