Slicing Swift collections.

Tram Ho

  • One of Swift’s goals is to become a highly applicable programming language from task high-level , UI construction, scripting , to low-level system programming. It’s an ambitious and unfulfilled goal, but there are some features of Swift that make it easy to expand.
  • One requirement is how the standard library works with its built-in collections most effectively by reducing the number of instances when their element are copied and moved.

1 / A slice of a binary:

  • In Swift, a slice is a special collection type that does not actually store any element of its own but act as a proxy (or view ) to allow us to access and work with a subset of a Other separate collections.
  • For example, we have an array of 10 numbers and we want to get the first 5 numbers to work. This is done using Range-based subscripting , as follows:

  • At first glance, firstFive will have the same type with numbers as Array<Int> . In fact, what we did above is create a slice of ArraySlice<Int>
  • Instead of copying 5 input elements into a new Array instance , instead, the standard library only gives us a glimpse into the range of element help increase performance significantly, especially when working with large collection than.
  • By not performing any duplication or allocating additional memory to the collection , a slice can be instantiated in constant time (O (1). That helps create a faster slice + create slice as if we were doing it on the original collection .

2 / Prefixes and suffixes:

  • Let’s start by looking at how we can use slicing to get prefixes and suffixes from the collection . For example, we work on TodoApp :

  • Now, we are building a feature that allows our user to quickly see the first three items in a specific list – for example, in the TodayApp extension on iOS or macOS . We can use the same registration API as we used when slicing array our upper slicing array :

  • Although the above syntax is very neat, it is a quite dangerous implementation . Because we can know how many TodoList item will actually be used, our app may crash when accessing the above property because just like when retrieving an element from an array, range registration also causes problems when used with parts from out of range .
  • While we can test our own limits on implementation :

  • Now our new API will work as expected, even if TodoList contains fewer than 3 item and our implementation still has O (1) complexity, meaning we can be comfortable computed property it. without the risk of a performance decrease.
  • However, one thing we must keep in mind when working with slice is that they are actually a separate type from the original collection meaning we can pass an ArraySlice to any API . accept Array and vice versa. It gives us full control when a slice is separated (and its element are copied) from its original collection .
  • For example, we use a different way of the standard-ibrary API prefix (along with its suffix ) to divide a package into two separate packages based on the index . Since we don’t want our Shipment model to contain ArraySlice , we have to convert our two slice into an Array<Package> :

  • We have calculated the prefix and suffix of them tadua on the number of particles and the index, but we can also use custom logic. For example, we call the prefix by:

3 / Dropping elements:

  • For example, we want to remove any numbers that appear at the beginning of the string , to prepare a string be used as a standard identifier. We can ask string out all the element in the element current is a number :

  • One of the main benefits of slicing based API collection in Swift is that they can be initialized without copying. That’s the benefit we have in situations like the one below:

  • Let’s look at how to combine dopFirst with the prefix to easily add paging support to any BidirectionalCollection (including types like Array, Range, etc.). By calling dropFirst first to remove all the elements before the current page starts and then use the prefix to extract a slice of the same size as our page size, we can deploy Pagination extensions like this:

  • Going back to our TodoList type from earlier, we could then wrap the API above on a slightly higher level of abstraction, giving us a really nice paging method that can be used to display. Show any to-do list in a modal page:

  • At first it may seem strange to return an ArraySlice from the above API , instead of converting the result into an appropriate Array . However, we have followed the same rules as the standard library that allows the site to decide how and when to convert each slice that we can perform additional string without string performance.
Share the news now

Source : Viblo