How to structure swift code

Coming from C++, I have not yet understood two important things in Swift:

  1. How do you structure the code in swift without headers in large projects without loosing track of the main interfaces or putting implementations details where it shouldn't?

  2. I've seen some popular approach of writing code in swift where you define a struct or class holding the relevant data and then using extension to define inits, properties and functions. Is this a good practice? It seems at first sight difficult to navigate through many extensions just to understand what this struct is supposed to do. Why not define the init right away if you are introducing a new type?

There’s no one canonical way, but here are some articles that provide some reasonable perspectives:

https://www.hackingwithswift.com/quick-start/understanding-swift/when-should-you-use-extensions-in-swift

1 Like

Also, note that even though Swift doesn’t have header files, you can view the generated interface of your code. Here’s a bit of a dated document on how to do this in Xcode:

https://developer.apple.com/library/archive/qa/qa1914/_index.html

1 Like
Terms of Service

Privacy Policy

Cookie Policy