0
0
iOS Swiftmobile~5 mins

Collections (Array, Dictionary, Set) in iOS Swift

Choose your learning style9 modes available
Introduction

Collections help you store and organize many pieces of data in one place. They make it easy to find, add, or change data.

You want to keep a list of your favorite songs.
You need to store names and phone numbers together.
You want to keep a group of unique items, like a set of visited cities.
You want to quickly find a value using a key, like a username and password.
You want to loop through a group of items to show them on the screen.
Syntax
iOS Swift
var array: [Type] = [value1, value2, ...]
var dictionary: [KeyType: ValueType] = [key1: value1, key2: value2, ...]
var set: Set<Type> = [value1, value2, ...]

Arrays keep items in order and allow duplicates.

Dictionaries store key-value pairs for fast lookup by key.

Sets store unique items without order.

Examples
An array of strings holding fruit names in order.
iOS Swift
var fruits: [String] = ["Apple", "Banana", "Cherry"]
A dictionary with names as keys and phone numbers as values.
iOS Swift
var phoneBook: [String: String] = ["Alice": "123-4567", "Bob": "987-6543"]
A set of integers. The duplicate 3 is stored only once.
iOS Swift
var uniqueNumbers: Set<Int> = [1, 2, 3, 3]
Examples of empty collections ready to be filled later.
iOS Swift
var emptyArray: [Int] = []
var emptyDictionary: [String: Int] = [:]
var emptySet = Set<String>()
Sample App

This SwiftUI app shows three collections: an array of fruits, a dictionary of phone numbers, and a set of unique numbers. It displays each collection's items in a list.

iOS Swift
import SwiftUI

struct ContentView: View {
    @State private var fruits: [String] = ["Apple", "Banana", "Cherry"]
    @State private var phoneBook: [String: String] = ["Alice": "123-4567", "Bob": "987-6543"]
    @State private var uniqueNumbers: Set<Int> = [1, 2, 3, 3]

    var body: some View {
        VStack(alignment: .leading, spacing: 20) {
            Text("Fruits Array:")
                .font(.headline)
            ForEach(fruits, id: \.self) { fruit in
                Text(fruit)
            }

            Text("Phone Book Dictionary:")
                .font(.headline)
            ForEach(phoneBook.sorted(by: { $0.key < $1.key }), id: \.key) { name, number in
                Text("\(name): \(number)")
            }

            Text("Unique Numbers Set:")
                .font(.headline)
            ForEach(Array(uniqueNumbers).sorted(), id: \.self) { number in
                Text(String(number))
            }
        }
        .padding()
    }
}

@main
struct CollectionsApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}
OutputSuccess
Important Notes

Arrays keep the order of items and allow duplicates.

Dictionaries let you find values quickly using keys.

Sets store only unique items and do not keep order.

Use arrays when order matters, dictionaries when you need key-value pairs, and sets when you want unique items.

Summary

Arrays, dictionaries, and sets are basic ways to store groups of data.

Arrays keep order and allow duplicates.

Dictionaries store data as key-value pairs for fast lookup.

Sets store unique items without order.