Instance Method

append(_:)

Adds an element to the end of the collection.

Declaration

iOS, macOS, tvOS, watchOS
mutating func append(_ newElement: Character)

Parameters

newElement

The element to append to the collection.

Discussion

If the collection does not have sufficient capacity for another element, additional storage is allocated before appending newElement. The following example adds a new number to an array of integers:

var numbers = [1, 2, 3, 4, 5]
numbers.append(100)

print(numbers)
// Prints "[1, 2, 3, 4, 5, 100]"

Complexity: O(1) on average, over many additions to the same collection.

Relationships

See Also

Appending Strings and Characters

func append(String)

Appends the given string to this string.

func append<S>(contentsOf: S)

Appends the characters in the given sequence to the string.

func append<S>(contentsOf: S)

Adds the elements of a sequence or collection to the end of this collection.

Beta
func appending(String)

Returns a new string made by appending a given string to the String.

func appendingFormat(String, CVarArg...)

Returns a string made by appending to the String a string constructed from a given format string and the following arguments.

func padding(toLength: Int, withPad: String, startingAt: Int)

Returns a new string formed from the String by either removing characters from the end, or by appending as many occurrences as necessary of a given pad string.

func reserveCapacity(Int)

Reserves enough space in the string’s underlying storage to store the specified number of ASCII characters.