Loading...
Preparing your coding drills
Comprehensive reference for 31 Swift methods across 6 categories.
append
Adds a new element to the end of the array. The array must be declared with var to allow mutation.
remove(at:)
Removes and returns the element at the specified position. The index must be valid.
contains
Returns a Boolean value indicating whether the sequence contains the given element. The element type must conform to Equatable.
sorted
Returns the elements of the sequence sorted in ascending order. The element type must conform to Comparable.
map
Returns an array containing the results of mapping the given closure over the sequence elements.
filter
Returns an array containing only the elements of the sequence that satisfy the given predicate.
reduce
Returns the result of combining the elements of the sequence using the given closure, starting with an initial value.
compactMap
Returns an array containing the non-nil results of calling the given transformation with each element of the sequence.
hasPrefix
Returns a Boolean value indicating whether the string begins with the specified prefix.
hasSuffix
Returns a Boolean value indicating whether the string ends with the specified suffix.
lowercased
Returns a lowercase version of the string.
uppercased
Returns an uppercase version of the string.
split(separator:)
Returns the longest possible subsequences of the string split around the given separator.
updateValue(_:forKey:)
Updates the value stored in the dictionary for the given key, or adds a new key-value pair if the key does not exist. Returns the old value if the key existed.
removeValue(forKey:)
Removes the given key and its associated value from the dictionary. Returns the removed value, or nil if the key was not found.
mapValues
Returns a new dictionary containing the keys of this dictionary with the values transformed by the given closure.
merge(_:uniquingKeysWith:)
Merges the key-value pairs from another dictionary or sequence into this dictionary, using a combining closure for duplicate keys.
insert (Set)
Inserts the given element into the set if it is not already present. Returns a tuple indicating whether the element was inserted and the element after insertion.
union
Returns a new set with the elements of both this set and the given sequence.
intersection
Returns a new set with the elements that are common to both this set and the given sequence.
Optional map
Evaluates the given closure when this Optional instance is not nil, passing the unwrapped value as a parameter. Returns nil if the optional is nil.
Optional flatMap
Evaluates the given closure when this Optional instance is not nil, passing the unwrapped value as a parameter. Unlike map, the closure itself returns an Optional, and flatMap prevents double-wrapping.
nil coalescing (??)
Returns the unwrapped value of the optional if it is not nil, otherwise returns the default value. The right-hand side is evaluated lazily.
optional chaining
Queries and calls properties, methods, and subscripts on an optional that might currently be nil. If the optional contains a value, the call succeeds; if it is nil, the call returns nil.
forEach
Calls the given closure on each element in the sequence in the same order as a for-in loop.
first(where:)
Returns the first element of the sequence that satisfies the given predicate.
allSatisfy
Returns a Boolean value indicating whether every element of the sequence satisfies the given predicate.
contains(where:)
Returns a Boolean value indicating whether the sequence contains an element that satisfies the given predicate.
enumerated
Returns a sequence of pairs (n, x), where n represents a consecutive integer starting at zero and x represents an element of the sequence.
min
Returns the minimum element in the sequence. The element type must conform to Comparable.
max
Returns the maximum element in the sequence. The element type must conform to Comparable.