Enums
An enum is considered as a structured data type that can be modified without needing to change say a String or Int multiple times within your code, for example the below shows how easy it would be to change something by accident and forget to change it somewhere else.
Structs
With a enum we can avoid this and never have to worry about changing the same thing more than once
Structs
I'm not sure how much you know about the MVC pattern but in Swift this is a common practise, before I explain how structs are useful I'll give a quick overview of MVC in Swift.
Model - struct, useful to managing large amounts of data View - Anything that extends UIView, more often than not this is a controller you manage on the storyboard Controller - class, typically used only for views such as UIView controllers and UITableView
Moving on a struct as I said is used for managing large amounts of data, for instance humans are a good example as we can use a struct to manage each person in a contact list.
Classes
Classes
More often than not you would only find classes bound views, when bound to a view iOS will automatically assign a new instance of the class whenever a view is called, the second time the view is called it requests the already created instance of the class.
Other uses for a class is utility helpers which you can create as singletons, this is more of an advanced concept and generally you only need to create code like this on larger applications as generally everything you need is already built-in however it's recommend if you do need additional functionality that you use an extension which allows you to add to any built in object and create your own subscripts
Comments
Post a Comment