Swift Tutorial

Swift Data Types

Swift Control Flow

Swift Functions

Swift Collections

Swift OOPs

Swift Additional Topics

Swift - Arrays Properties

Arrays in Swift are a fundamental collection type with a variety of properties and methods that enable developers to interact with and manipulate them. Here's a detailed look at the core properties of arrays in Swift:

1. count:

This property returns the number of elements in the array.

let fruits = ["apple", "banana", "cherry"]
print(fruits.count)  // Outputs: 3

2. isEmpty:

This property is a boolean value indicating whether the array is empty (i.e., whether its count is zero).

let emptyArray: [String] = []
print(emptyArray.isEmpty)  // Outputs: true

3. startIndex:

Returns the first index of the array. For standard arrays, this will always be 0, but it's useful to be aware of this property because some collection types might not always start at zero.

let numbers = [10, 20, 30, 40]
print(numbers.startIndex)  // Outputs: 0

4. endIndex:

This property represents the index after the last element of the array. It's important to note that endIndex isn't a valid index to use for subscripting; it's typically used for range operations.

let values = [1, 2, 3]
print(values.endIndex)  // Outputs: 3

5. first:

This property returns the first element of the array if the array is not empty; otherwise, it returns nil.

let animals = ["cat", "dog", "bird"]
print(animals.first)  // Outputs: Optional("cat")

6. last:

This property returns the last element of the array if the array is not empty; otherwise, it returns nil.

print(animals.last)  // Outputs: Optional("bird")

7. capacity:

This property returns the total number of elements that the array can contain without allocating new storage.

var numbersArray = [1, 2, 3, 4, 5]
print(numbersArray.capacity)  // The value may vary depending on the internal storage allocated by Swift.

These properties provide essential information about the state and structure of an array. When combined with the array methods available in Swift, you can efficiently manipulate and query arrays for a wide range of tasks.

  1. Array size and capacity in Swift:

    • Description: The size of an array is determined by the number of elements it contains, while the capacity is the amount of space allocated for the array's elements.

    • Code:

      var numbers = [1, 2, 3, 4, 5]
      
      // Size of the array
      let size = numbers.count
      
      // Capacity of the array
      let capacity = numbers.capacity
      
  2. Checking if an array is empty in Swift:

    • Description: You can check if an array is empty using the isEmpty property.

    • Code:

      var fruits: [String] = []
      
      // Checking if the array is empty
      if fruits.isEmpty {
          print("The array is empty.")
      } else {
          print("The array is not empty.")
      }
      
  3. Swift array count property:

    • Description: The count property returns the number of elements in an array.

    • Code:

      let colors = ["Red", "Green", "Blue"]
      
      // Getting the count of elements
      let count = colors.count
      
  4. Capacity of an array in Swift:

    • Description: The capacity property returns the total number of elements that the array can contain without allocating more memory.

    • Code:

      var elements = [1, 2, 3, 4, 5]
      
      // Getting the capacity of the array
      let capacity = elements.capacity
      
  5. Finding the first and last elements in Swift array:

    • Description: You can use the first and last properties to retrieve the first and last elements of an array.

    • Code:

      let numbers = [10, 20, 30, 40, 50]
      
      // Finding the first and last elements
      let firstElement = numbers.first
      let lastElement = numbers.last
      
  6. Swift array isEmpty property:

    • Description: The isEmpty property returns a Boolean value indicating whether the array is empty.

    • Code:

      var books: [String] = []
      
      // Checking if the array is empty
      if books.isEmpty {
          print("The array is empty.")
      } else {
          print("The array is not empty.")
      }
      
  7. Determine if an element exists in Swift array:

    • Description: You can check if a specific element exists in an array using the contains method.

    • Code:

      let cities = ["New York", "London", "Paris"]
      
      // Checking if an element exists
      let hasLondon = cities.contains("London")
      
  8. Swift array first and last properties:

    • Description: The first property returns the first element of the array, and the last property returns the last element.

    • Code:

      let fruits = ["Apple", "Banana", "Orange"]
      
      // Getting the first and last elements
      let firstFruit = fruits.first
      let lastFruit = fruits.last
      
  9. Properties for checking uniqueness in Swift arrays:

    • Description: Swift arrays do not have built-in properties for checking uniqueness. However, you can use sets or implement custom logic to check for unique elements.

    • Code:

      let numbers = [1, 2, 3, 1, 4, 5]
      
      // Using a Set to check uniqueness
      let uniqueNumbers = Set(numbers)