Skip to main content

What's the difference between enum, struct and class?


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.

let myString = "test"

if myString == "ttest" {
  // Doesn't execute because "ttest" is the value assigned to "myString"
}


With a enum we can avoid this and never have to worry about changing the same thing more than once

enum MyEnum: String {
  case Test = "test"
}

let enumValue = MyEnum.Test

if enumValue == MyEnum.Test {
  // Will execute because we can reassign the value of "MyEnum.Test" unless we do so within "MyEnum"
}


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.


struct Birthday {
  var day: Int
  var month: Int
  var year: Double
}

struct Person {
  var firstName: String
  var lastName: String
  var birthday: Birthday
  var phoneNumber: String
  var emailAddress: Int
}

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

Popular posts from this blog

Understanding of Size Classes?

Size Classes are an abstraction of how a device should be categorized depending on its screen dimensions. Apple defined two categorizations for both vertical and horizontal sizes called “Regular” and “Compact”. The former specifies a big space, while the latter specifies a small” space. How big or small exactly? Here, “big” and “small” are not intended to be measured in inches. iOS Size Classes Apple has introduced many devices like iPhone, iPad with different screen sizes and resolution. Also after iOS 8 apple has supported multitasking in iPad. So for the developers to develop a common or single UI for all the devices apple has introduced the concept of an adaptive layout by combining auto layout and size classes. What is adaptive layout: The adaptive layout is a method of building the apps based on the size and characteristics of the container instead of a targeting a particular device. We can create a single layout to work on all...

The differences between Core Data and a Database

Database Core Data Primary function is storing and fetching data Primary function is graph management (although reading and writing to disk is an important supporting feature) Operates on data stored on disk (or minimally and incrementally loaded) Operates on objects stored in memory (although they can be lazily loaded from disk) Stores "dumb" data Works with fully-fledged objects that self-manage a lot of their behavior and can be subclassed and customized for further behaviors Can be transactional, thread-safe, multi-user Non-transactional, single threaded, single user (unless you create an entire abstraction around Core Data which provides these things) Can drop tables and edit data without loading into memory Only operates in memory Perpetually saved to disk (and often crash resilient) Requires a save process Can be slow to create millions of new rows Can create millions of new objects in-memory very quickly (although saving these objects will be slow) Offer...

What Is a Closure?

Closures are self contained chunks of code that can be passed around and used in your code. Closures can capture and store references to any constants or variables from the context in which they are defined. This is know as closing over those variables, hence the name closures. Closures are use intensively in the Cocoa frameworks – which are used to develop iOS or Mac applications. Functions are a special kind of closures. There are three kinds of closures: global functions  – they have a name and cannot capture any values nested functions  – they have a name and can capture values from their enclosing functions closure expressions  – they don’t have a name and can capture values from their context The thing to keep in mind for the moment is that you already have an intuition about closures. They are almost the same as functions but don’t necessarily have a name. // a closure that has no parameters and return a String var hello: () -> ( String ) = { ...