Tutorial by Examples

Copying an array will copy all of the items inside the original array. Changing the new array will not change the original array. var originalArray = ["Swift", "is", "great!"] var newArray = originalArray newArray[2] = "awesome!" //originalArray = ["...
Array is an ordered collection type in the Swift standard library. It provides O(1) random access and dynamic reallocation. Array is a generic type, so the type of values it contains are known at compile time. As Array is a value type, its mutability is defined by whether it is annotated as a var (...
The following examples will use this array to demonstrate accessing values var exampleArray:[Int] = [1,2,3,4,5] //exampleArray = [1, 2, 3, 4, 5] To access a value at a known index use the following syntax: let exampleOne = exampleArray[2] //exampleOne = 3 Note: The value at index two is th...
Determine whether an array is empty or return its size var exampleArray = [1,2,3,4,5] exampleArray.isEmpty //false exampleArray.count //5 Reverse an Array Note: The result is not performed on the array the method is called on and must be put into its own variable. exampleArray = exampleArray...
There are multiple ways to append values onto an array var exampleArray = [1,2,3,4,5] exampleArray.append(6) //exampleArray = [1, 2, 3, 4, 5, 6] var sixOnwards = [7,8,9,10] exampleArray += sixOnwards //exampleArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] and remove values from an array exampleAr...
var array = [3, 2, 1] Creating a new sorted array As Array conforms to SequenceType, we can generate a new array of the sorted elements using a built in sort method. 2.12.2 In Swift 2, this is done with the sort() method. let sorted = array.sort() // [1, 2, 3] 3.0 As of Swift 3, it has...
As Array conforms to SequenceType, we can use map(_:) to transform an array of A into an array of B using a closure of type (A) throws -> B. For example, we could use it to transform an array of Ints into an array of Strings like so: let numbers = [1, 2, 3, 4, 5] let words = numbers.map { Stri...
The things Array contains values of Any type. let things: [Any] = [1, "Hello", 2, true, false, "World", 3] We can extract values of a given type and create a new Array of that specific type. Let's say we want to extract all the Int(s) and put them into an Int Array in a safe ...
You can use the filter(_:) method on SequenceType in order to create a new array containing the elements of the sequence that satisfy a given predicate, which can be provided as a closure. For example, filtering even numbers from an [Int]: let numbers = [22, 41, 23, 30] let evenNumbers = number...
You can use flatMap(_:) in a similar manner to map(_:) in order to create an array by applying a transform to a sequence's elements. extension SequenceType { public func flatMap<T>(@noescape transform: (Self.Generator.Element) throws -> T?) rethrows -> [T] } The difference with...
One can extract a series of consecutive elements from an Array using a Range. let words = ["Hey", "Hello", "Bonjour", "Welcome", "Hi", "Hola"] let range = 2...4 let slice = words[range] // ["Bonjour", "Welcome", &quot...
If we have a struct like this struct Box { let name: String let thingsInside: Int } and an array of Box(es) let boxes = [ Box(name: "Box 0", thingsInside: 1), Box(name: "Box 1", thingsInside: 2), Box(name: "Box 2", thingsInside: 3), B...
As well as being able to create an array by filtering out nil from the transformed elements of a sequence, there is also a version of flatMap(_:) that expects the transformation closure to return a sequence S. extension SequenceType { public func flatMap<S : SequenceType>(transform: (Sel...
3.0 The most simple way is to use sorted(): let words = ["Hello", "Bonjour", "Salute", "Ahola"] let sortedWords = words.sorted() print(sortedWords) // ["Ahola", "Bonjour", "Hello", "Salute"] or sort() var mutable...
We can use flatten() in order to lazily reduce the nesting of a multi-dimensional sequence. For example, lazy flattening a 2D array into a 1D array: // A 2D array of type [[Int]] let array2D = [[1, 3], [4], [6, 8, 10], [11]] // A FlattenBidirectionalCollection<[[Int]]> let lazilyFlatten...
reduce(_:combine:) can be used in order to combine the elements of a sequence into a single value. It takes an initial value for the result, as well as a closure to apply to each element – which will return the new accumulated value. For example, we can use it to sum an array of numbers: let numbe...
Generally, if we want to remove an element from an array, we need to know it's index so that we can remove it easily using remove(at:) function. But what if we don't know the index but we know the value of element to be removed! So here is the simple extension to an array which will allow us to re...
2.12.2 You can use the minElement() and maxElement() methods to find the minimum or maximum element in a given sequence. For example, with an array of numbers: let numbers = [2, 6, 1, 25, 13, 7, 9] let minimumNumber = numbers.minElement() // Optional(1) let maximumNumber = numbers.maxElement()...
By adding the following extension to array indices can be accessed without knowing if the index is inside bounds. extension Array { subscript (safe index: Int) -> Element? { return indices ~= index ? self[index] : nil } } example: if let thirdValue = array[safe: 2] { ...
The zip function accepts 2 parameters of type SequenceType and returns a Zip2Sequence where each element contains a value from the first sequence and one from the second sequence. Example let nums = [1, 2, 3] let animals = ["Dog", "Cat", "Tiger"] let numsAndAnimals ...

Page 1 of 1