Autumn  Blick

Autumn Blick


Swift: Alternative to struct

Imagine that you are creating an app which should, among other things, track analytics events. For this you have chosen one of the event tracking systems (e.g. Firebase).

The first idea would be to create a struct that will model AnalyticsEvent .

struct AnalyticsEvent {
   let userId: String
   let hasAdditionalData: Bool
   let additionalData: [String: String]
   let hasFeedback: Bool
   let feedbackContent: String
   // rest of the properties

Although this is a hypothetical situation, it highlights a problem. Because struct can contain multiple values, bugs may arise if multiple events are contained in one command (e.g. hasFeedback and hasAdditionalData). This reveals a problem of not having mutually exclusive properties, as event can be in two different states at the same time.

Using an enum to group the properties into cases makes data much cleaner. Swift offers a construct where you can have values associated with types in order to build more complex data structures.

enum AnalyticsEvent {
   case sendFeedback(userId: String, content: String)
   case sendAdditionalData(userId: String, data: [String: String]
   case connectAppleHealth(userId: String)
   // rest of the cases

By adding tuples to each of the cases it makes it more clear which properties belong together. Now a proper event can be constructed without worrying about mixing and matching wrong values.

let feedbackEvent = 
    AnalyticsEvent.sendFeedback(userId: "id", content: "content")

With this type of construct a switch case can be used for unwrapping inner values:

func logEvent(event: AnalyticsEvent) {
   switch event {
      case .sendFeedback(_: String, content: String): 
      // rest of the cases
if case let AnalyticsEvent.sendFeedback(_, content) = event {

By using switch you need to exhaust all of the cases for AnalyticsEvent. If you are interested in specific case you can utilize if case let construct. Also a match on specific property can be done with a wild card operator, with which you are telling that you don’t care about the value of the specific property.

#ios-app-development #swift #ios #structures

What is GEEK

Buddha Community

Swift: Alternative to struct
Houston  Sipes

Houston Sipes


10 Free Online Resources To Learn Swift Language

Swift is a fast and efficient general-purpose programming language that provides real-time feedback and can be seamlessly incorporated into existing Objective-C code. This is why developers are able to write safer, more reliable code while saving time. It aims to be the best language that can be used for various purposes ranging from systems programming to mobile as well as desktop apps and scaling up to cloud services.

Below here, we list down the 10 best online resources to learn Swift language.

(The list is in no particular order)

#developers corner #free online resources to learn swift language #learn swift #learn swift free #learn swift online free #resources to learn swift #swift language #swift programming

Top Swift Development Companies | Top Swift Developers -

A thoroughly researched list of top Swift developers with ratings & reviews to help find the best Swift development companies around the world.

#swift development service providers #best swift development companies #top swift development companies #swift development solutions #top swift developers #swift

Hire Dedicated Swift Developers

Want to create a native iOS application for your Startup?

Hire Dedicated Swift Developers for end-to-end services like development, migration, upgrade, testing, and support & maintenance. Trust our Swift development team for iOS device apps that are high on performance and security.

Consult with experts:-

#hire dedicated swift developers #swift developers #swift development company #swift development services #swift development #swift

Best Swift App Development Company

Hire an Exceptional Swift App Developer from Mobile App Development India. Maadi has a dedicated Swift App Development team that is superiorly talented and builds highly functional, cost-effective mobile apps with error-free coding.


#swift ios app development india #hire swift programmer india #swift ios development #apple swift app development #swift mobile app development #swift app development

Hire Expert Swift iOS App Developers in USA

Are you looking to hire the best swift iOS developers for your iPhone or iPad App project? AppClues Infotech is a top-rated iOS app development company in the USA. Hire our dedicated swift iOS app developers to build feature-rich and robust iOS app.

For more info:
Call: +1-978-309-9910

#top swift app development company usa #best swift app development company #swift app development #swift ios app development #swift app development company #hire expert swift ios app developers in usa