Swift – Append String to Array
To append a String value to a Swift array, call the array’s append(_:) method. The array must be declared with var because appending changes the array.
The new string is added at the end of the array, and the array’s count increases by one.
Swift String Array append() Syntax
Following is a quick example to add a string value to the array using append() function.
array_name.append(string_value)
where
array_name is the String Array
string_value is the string value appended to this array
After appending, the size of the array increases by 1.
Example 1 – Append a String to an Array in Swift
In the following example, we shall define an array with three string values and using append() function, we will append a string to the array.
main.swift
var names:[String] = ["TutorialKart","Swift Tutorial", "iOS Tutorial"]
names.append("New String")
var name = names[3]
print( "New size of names array is \(names.count)" )
print( "Value of string at index 3 is \(name)" )
Output
New size of names array is 4
Value of string at index 3 is New String
After appending a string, the size of the array has been incremented by 1.
Append Multiple Strings to a Swift Array
Use append(contentsOf:) when you need to add every element from another sequence or array. The strings are appended in their existing order.
var languages = ["Swift", "Kotlin"]
let moreLanguages = ["Java", "Python"]
languages.append(contentsOf: moreLanguages)
print(languages)
Output
["Swift", "Kotlin", "Java", "Python"]
Use append(_:) for one string and append(contentsOf:) for multiple strings. Passing an array directly to append(_:) does not work for an array whose element type is String, because the method expects one String, not [String].
Append One Swift String Array to Another with +=
The += operator is another concise way to append all strings from one array to another.
var primaryColors = ["Red", "Blue"]
let additionalColors = ["Green", "Yellow"]
primaryColors += additionalColors
print(primaryColors)
Output
["Red", "Blue", "Green", "Yellow"]
Append a String Only When It Is Not Already in the Array
The append(_:) method allows duplicate values. To avoid adding the same string twice, check the array with contains(_:) before appending.
var fruits = ["Apple", "Banana"]
let newFruit = "Apple"
if !fruits.contains(newFruit) {
fruits.append(newFruit)
}
print(fruits)
Output
["Apple", "Banana"]
String comparison is case-sensitive. For example, "Apple" and "apple" are different values unless you normalize or compare them without regard to case.
Swift Array Appending Errors to Check
- The array is declared with let: A constant array cannot be changed. Declare it with
varbefore callingappend(_:). - The value has the wrong type: A
[String]array accepts onlyStringelements. Convert other values to strings when appropriate. - An array is passed to append(_:): Use
append(contentsOf:)or+=to add multiple strings. - An index is read before appending: Append the value first, or use
lastwhen you only need the newly added final element.
Swift String Array append() FAQs
Does append() add a String at the beginning or end of a Swift array?
append(_:) adds the string at the end of the array. Use insert(_:at:) when the string must be placed at a specific index, including index 0.
Can I append a String to an array declared with let?
No. An array declared with let is constant and cannot be modified. Declare the array with var to append or remove elements.
How do I append an array of strings to another Swift array?
Call append(contentsOf:) or use the += operator. Both add the source array’s elements to the destination array rather than nesting the source array as one element.
How do I get the String that was just appended?
After appending, read the array’s last property. It returns an optional value because an array can be empty.
var names = ["Asha", "Ravi"]
names.append("Meera")
if let appendedName = names.last {
print(appendedName)
}
Editorial QA Checklist for Swift String Array Appending
- Confirm that single-value examples use
append(_:)with aString. - Confirm that multi-value examples use
append(contentsOf:)or+=. - Verify that every mutated array is declared with
var. - Check that printed indexes are valid after the append operation.
- Run each example and compare its result with the displayed output.
Swift String Array append() Summary
In this Swift Tutorial, we have learned to append one string with array.append(value), append multiple strings with append(contentsOf:), combine arrays with +=, and prevent duplicate strings with contains(_:).
TutorialKart.com