Programming architecture patterns For Swift

To use basic knowledge of Swift better! I want to kown more Programming architecture patterns For Swift, And I really want to use them in my project to make my code cleaner and more efficient!

First, your data should be manipulated threw a Swift package. An internal Swift package is OK.

You can use repositories for your data using protocols:

  • a repository linked to a web service / a database
  • a repository linked to memory and mocked data.

Once you can run all the tests on your data, you can go to app stack.

On your app, you can use dependencies inversion (or injection, as you want).

A container will store repositories for all your app, but repositories type will be generic, based on protocol. If you run your app in tests mode (ProcessInfo provides environment variables to configure it), you can use repository with mocked data. It can also be useful with Xcode scheme with a "Preview" scheme that run the app in mocked environment.

Your views (or view controllers) will have associated models. MVVM principle. The model for view transforms specific data in UI data (ie. Date -> String). View is initialized with a model as parameter.

And view model is initialized with a generic repository. View model will access to data without knowing kind of source.

Finally, container that store repositories will build the view model and view by injecting repositories in view models, and injecting configured view models in view and return configured view.

With that, you will have an easy way to test your UI and a good separation of concerns.

And, if you need to create an extension (Widget, Siri) or an other app (iOS, macOS), you will be able to reuse Swift package.

Final advice: use security groups to share content between app and extensions. Even if it's not necessary while you build your app.

Programming architecture patterns For Swift
 
 
Q