1594958820
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):
print("\(content)")
// rest of the cases
}
if case let AnalyticsEvent.sendFeedback(_, content) = event {
print("\(content)")
}
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
1600430400
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
1609999986
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
1594193714
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 HourlyDeveloper.io our Swift development team for iOS device apps that are high on performance and security.
Consult with experts:- https://bit.ly/2C5M6cz
#hire dedicated swift developers #swift developers #swift development company #swift development services #swift development #swift
1603285318
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.
Contact: https://www.mobile-app-development-india.com/swift-app-development/
#swift ios app development india #hire swift programmer india #swift ios development #apple swift app development #swift mobile app development #swift app development
1612441441
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:
Website: https://www.appcluesinfotech.com/
Email: info@appcluesinfotech.com
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