101.school
CoursesAbout
Search...⌘K
Generate a course with AI...

    Learn Swift Step By Step

    Receive aemail containing the next unit.
    • Introduction to Swift
      • 1.1Swift Language Overview
      • 1.2Basic Syntax in Swift
      • 1.3Data Types and Variables
      • 1.4Operators and Control Flow in Swift
    • Classes,Properties and Methods in Swift
      • 2.1Introduction to Classes and Objects
      • 2.2Properties
      • 2.3Methods
      • 2.4Inheritance, Polymorphism & Protocols
    • Collection Types in Swift
      • 3.1Introduction to Arrays, Sets and Dictionaries
      • 3.2Basic Collection Operations
      • 3.3Looping Over Collections & Modifying Collections
      • 3.4Advanced Collection Types
    • Advanced Swift
      • 4.1Error Handling in Swift
      • 4.2Extensions and Protocols
      • 4.3Generics
      • 4.4Concurrency and Multi-Threading in Swift

    Collection Types in Swift

    Introduction to Arrays, Sets, and Dictionaries in Swift

    In Swift, as in many other programming languages, collections are used to group related items together. The three primary collection types in Swift are arrays, sets, and dictionaries. This article will provide an introduction to these collection types and how to use them.

    Arrays

    An array is an ordered collection of items. The items in an array are all of the same type, and they are indexed by integers starting from zero.

    To create an array in Swift, you can use the following syntax:

    var someArray = [SomeType]()

    For example, to create an array of integers, you would write:

    var someInts = [Int]()

    You can also create an array with default values:

    var threeDoubles = Array(repeating: 0.0, count: 3)

    To access elements in an array, you use the index of the item:

    print(someInts[0]) // Prints the first element

    Sets

    A set is an unordered collection of unique items. Like arrays, the items in a set are all of the same type.

    To create a set in Swift, you can use the following syntax:

    var someSet = Set<SomeType>()

    For example, to create a set of strings, you would write:

    var someStrings = Set<String>()

    Since sets are unordered, you can't access items using an index like you can with arrays. Instead, you can check if a set contains a certain item:

    if someStrings.contains("someString") { print("someString is in the set.") }

    Dictionaries

    A dictionary is an unordered collection of key-value pairs. The keys in a dictionary are all of the same type, and the values are all of the same type.

    To create a dictionary in Swift, you can use the following syntax:

    var someDict = [KeyType: ValueType]()

    For example, to create a dictionary with string keys and integer values, you would write:

    var ages = [String: Int]()

    To access the value associated with a particular key, you use the key:

    print(ages["John"]) // Prints the value associated with the key "John"

    In conclusion, arrays, sets, and dictionaries are powerful tools in Swift that allow you to group related items together. Understanding how to use these collections is a fundamental part of programming in Swift.

    Test me
    Practical exercise
    Further reading

    My dude, any questions for me?

    Sign in to chat
    Next up: Basic Collection Operations