The “.count” property and the “.append()”, “.insert()” and “.remove()” methods in Swift

.count

The .count() property in Swift is used to return the number of elements in an array, dictionary, or string. Here is an example of using the “.count” property to count the number of elements in an array:

let array = [1, 2, 3, 4, 5]
let count = array.count
print(count)

// This will output "5"

Here is an example of using the .count() property to count the number of characters in a string:

let str = "Hello, world!"
let count = str.count
print(count)

// This will output "13"

In both examples, the .count() property is used to obtain the length of the array or String, respectively.

.append()

In Swift, the .append() method is used to add a new element to the end of an array. Here is an example of using thismethod to add a new element:

var myArray = [3, 7, 8, 1, 5]
myArray.append(2)
print(myArray)

// This will output "[3, 7, 8, 1, 5, 2]"

In this example, the .append() method is used to add the value 2 to the end of the “myArray” array. After the .append() method is called, “myArray” will have a length of 6 and will contain the values 3, 7, 8, 1, 5, and 2.

  • Note that the .append() method modifies the original array and does not return a new array.
  • Also, you can only use the .append() method on arrays that are declared with the “var” keyword (i.e. mutable arrays).

.insert()

In Swift, the .insert() method is used to insert a new element into an array at a specific index. Here is an example of using this method to insert a new element into an array:

var myArray = [3, 7, 8, 1, 5]
myArray.insert(2, at: 2)
print(myArray)

// This will output "[3, 7, 2, 8, 1, 5]"

In this example, the .insert() method is used to insert the value 2 at index 2 of the “myArray” array. After the .insert() method is called, “myArray” will have a length of 6 and will contain the values 3, 7, 2, 8, 1, and 5.

  • Note that like the .append() method, the .insert() method modifies the original array and does not return a new array.
  • Also, you can only use the .insert() method on arrays that are declared with the “var” keyword (i.e. mutable arrays).

.remove()

In Swift, the .remove() method is used to remove a specific element from an array, set or dictionary. Here are some examples of using it:

Example for an array

var myArray = [3, 7, 8, 1, 5]
let removedValue = myArray.remove(at: 2)
print(myArray)

// This will output "[3, 7, 1, 5]"

print(removedValue)

// This will output "8"

In this example, the .remove() method is used to remove the element at index 2 from the “myArray” array, which has a value of 8. After the .remove() method is called, “myArray” will have a length of 4 and will contain the values 3, 7, 1, and 5. The .remove() method returns the removed value, which is 8 in this case.

Example for a set

var mySet: Set = [3, 7, 8, 1, 5]
mySet.remove(8)
print(mySet)

// This will output "[3, 7, 1, 5]"

In this example, the .remove() method is used to remove the element with a value of 8 from the “mySet” set. After the .remove() method is called, “mySet” will contain the values 3, 7, 1, and 5.

Example for a dictionary

var myDict = ["name": "John", "age": 30, "city": "New York"]
let removedValue = myDict.removeValue(forKey: "age")
print(myDict)

// This will output "["name": "John", "city": "New York"]"

print(removedValue)

// This will output "30"

In this example, the .removeValue() method is used to remove the value associated with the key “age” from the “myDict” dictionary. After the .removeValue() method is called, “myDict” will contain the key-value pairs “name”: “John” and “city”: “New York”. The .removeValue() method returns the removed value, which is 30 in this case.

  • Note that like the other methods mentioned above, the .remove() method modifies the original collection and does not return a new collection.

Leave a Reply

Your email address will not be published. Required fields are marked *