Question: Swift: where to place IBAction functions for clean class code


Swift: where to place IBAction functions for clean class code

Answers 1
Added at 2017-01-05 11:01

What is your best practice with IBActions? Where do u place them? I cannnot decide.

1) If I place them with regular functions, it can be messy but everything if private.

2) When I place them as extension ClassName{} It is more clean BUT it can't work with private variables I have to make them unprivate.

I want to code clean. Thx

nr: #1 dodano: 2017-01-05 12:01

If I'm not mistaking, your case is when you add them to an extension, you won't be able to access private vars/methods.

You have (what I suggest) tow possible solutions:

1- If you want to keep them in a separated extension (but in the same .swift file) for the purpose of organizing your code, you need to change private to fileprivate.

For more information about access modifiers in Swift 3, you might want to check this answer.

2- Keep them in the ViewController and use // MARK: - IBActions, it marks sections in the symbol navigator.

For example:

enter image description here

As you can see, there are // MARK: - Core Data stack, // MARK: - Core Data Saving support and //MARK: - Machines Core Data to separate the code and make it more readable and organized.

Hope it helped.

Source Show
◀ Wstecz