Part 2: forEach

Tram Ho

A Flattening Map (33)

Usually when you want to query an array, the conversion function returns another array and not just one element. For example, we have a method, links , that can read the Markdown file and return an array containing the URL of all the paths in the file. That method looks like this:

If we have a bunch of Markdown files and want to extract the path from all the files into a single array, we can try to write some tests like . But it returns an array of an array containing URLs: one file for each array. Now you just practice map, get back an array of arrays and then flatten them to get a single array:

The flatMap function is a combination of 2 operators in 1 step. So markdownFiles.flatMap(extractLinks) returns all URLs in the array of each Markdown file as a single array.

Basically flatMap is similar to map , except that its input method is transformed into an array. To make it complete, it uses append (contentsOf 🙂 instead of append ( ? in flattening the resulting array:

Another better way to use flatMap is to combine elements from different arrays. To facilitate the combination of arrays, flatMap on one array and map on the other:

Iteration using forEach

The last operator we want to discuss is forEach. It works almost like a loop: The input method is executed each time for an object in the sequence. And unlike the map, forEach returns nothing. Let’s start with replacing loops with forEach:

There are no major changes, but it can be helpful if you make a call to each element in the string. Using the function named forEach instead of closure exoresion can be shorter and easier to understand. For example, if you are in a view controller and want to add a subview array to the main view, just write theViews.forEach (view.addSubview).

However, there are some minor differences between loops and forEach. Typically, if a loop has a return statement in it, rewriting it with forEach may not require significant code changes. Consider the example below, which is written using a loop with the where condition

We cannot copy the where statement directly in the forEach structure, so we can rewrite (not exactly the same) by using the filter:

The return value in the forEach closure does not return outside the function, it only returns inside its closure. In a practical case, we can detect a bug because the compiler will generate a warning about the unused argument being returned, but you should not rely on it to suggest a solution.

Also, consider the simple example below:

One thing that is not really clear is the printing of all numbers in the input range. The return statement does not break the loop, instead it goes back to the closure. In some situations, like the addSubview example above, forEach might be better than looping. However, since it is not broken by return, we only recommend it for its main function, in addition to using the regular loop instead.

Array types


Normally, to access a single element in an array with position (eg fibs [0]) we can also access multiple positions of an element via subscript. For example, to get all but the first object of the array, we use:

With slice arrays starting at the second element, the type of result returned is ArraySlice, not Array, ArraySlice is a view of the array. It is supported by the original array, however it provides many based on slices. This makes sure the array does not need replication. The ArraySlice type has the same identifier functions as the Array, so you can use a slice as an array if it was previously an array. If you want to convert a slice into an array, you need to structure a new array from the slice:


Swift arrays can be bridged with OBjective-C. It can also be used with C, but we can convert in subsequent chapters. Because NSArray can only hold symbolic teams, the compiler and runtime automatically wrap incompatible values ​​(such as enum) in an unknown object. Some value types (such as Int, Bool, and String, but including Dictionary and Set) are bridged by default with its object type in Objective – C

Share the news now

Source : Viblo