DEV Community

Donn
Donn

Posted on

3 1

Learning Swift Basics

I've started a new journey in becoming a mobile software engineer, specifically I'm learning Apple's Swift, a language used in their mobile app development, (and also their other platforms). I briefly started in Kotlin (used for Android app development), but I see a lot of overlap between the two languages.

Swift has some interesting ways to store and access data, including arrays, sets and dictionaries, to just name a few. We'll walk through the aforementioned datatypes, and explore their differences.

Array: Arrays should be a familiar topic, since every language seems to have this datatype. Arrays in Swift stores data in order, and allows for duplicates. Arrays only allow one type data, whether it's String, Int, Bool, etc.

Creating empty Arrays:

var array1: [String] = [String]()
var array2: [String] = []
var array3 = Array<String>()
Enter fullscreen mode Exit fullscreen mode

Set: Sets are very similar to arrays; both store only one type of data at a time, but some key differences are that sets have no set order in how they store data, and duplicates are not allowed in sets.

Creating empty Sets:

Var set = Set<String>()
Enter fullscreen mode Exit fullscreen mode

Dictionary: Dictionaries allows for customization in how data is stored in what is called "key: value" pairs. These pairings can be stored in a number of ways, such as [String: String], [Int: String], [Int: Bool]. Since Swift doesn't know how far any given dictionary extends, one extra step needs to be taken in case the key being accessed doesn't exist. When attempting to access the value of a key, a default value needs to be set:

dic["color", default: "Unknown"]

Creating empty Dictionaries

var dic1 = [String: String]()
var dic2 = Dictionary<String, Int>()
Enter fullscreen mode Exit fullscreen mode

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Image of Datadog

How to Diagram Your Cloud Architecture

Cloud architecture diagrams provide critical visibility into the resources in your environment and how they’re connected. In our latest eBook, AWS Solution Architects Jason Mimick and James Wenzel walk through best practices on how to build effective and professional diagrams.

Download the Free eBook

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay