iOS Fundamentals — iOS Tour Part 2 (Collection type)

Dipika Kansara
2 min readJan 14, 2022

In this tutorial you will learn some more basic iOS fundamentals like Array, Dictionaries, Sets, Enumerations (Enum).

Now let’s jump on our first topic in this fundamentals tour.

Topic — 1 Arrays:

In Swift, we do this grouping using an Array. Arrays are their own data type like String, Int, and Double

var name = ["Dipika", "Kansara"]
var numbers = [8, 7, 12, 5]

Swift actually counts an item’s index from zero rather than one and if your array is variable, you can modify it after creating it. For example, you can use append() to add new items

name.append("Middle Name required")

you can use .count to read how many items are in an array

print(name.count)

you can check whether an array contains a particular item by using contains()

print(name.contains("Middle Name"))

you can sort an array using sorted() you can change alphabetically for strings and numerically for numbers

print(name.sorted())
print(numbers.sorted())

you can reverse an array by calling reversed() on it

print(name.reveresed())

finally, you can remove items from an array by using either remove(at:) to remove one item at a specific index, or removeAll() to remove everything

name.remove(at: 2)
print(name.count)

name.removeAll()
print(name.count)

Topic — 2 Dictionary:

Dictionaries are used to store unordered lists of values of the same type.

You can create an empty dictionary of a certain type using the following initializer syntax −

var someDict = [KeyType: ValueType]()

You can use the following simple syntax to create an empty dictionary whose key will be of Int type and the associated values will be strings −

var dict = [Int: String]()

you can create a dictionary from a set of given values

let employee = [
"name": "Dipika",
"Position": "iOS Developer",
"age": 20
]

Topic — 3 Sets:

Sets are used to store values of same types but they don’t have definite ordering as arrays have.

you can create a set and modifiy it by inserting values in set.

var name = Set<String>()
name.insert("Dipika")
name.insert("Kansara")

Topic — 4Enumerations:

An enumeration is a user-defined data type which consists of set of related values. Keyword enum is used to defined enumerated data type.

It is declared in a class and its values are accessed through the instance of that class. Initial member value is defined using enum intializers. Its functionality is also extended by ensuring standard protocol functionality.

enum Weekday {
case monday
case tuesday
case wednesday
case thursday
case friday
}
var day = Weekday.monday
day = .tuesday

I hope covered topics here about iOS Basics.

Happy Coding 😁

--

--