Swift – Append String to Array
Welcome to Swift Tutorial. In this tutorial, we will learn how to append a String to an array in Swift.
To append a string to the String array in Swift, use the function append() on the array.
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.
Conclusion
In this Swift Tutorial, we have learned to append a string to String Array using array.append() function.