قالب وردپرس درنا توس
Home / IOS Development / Swift Tips: Mixing and matching important and functional code

Swift Tips: Mixing and matching important and functional code



Swift is a hybrid language: it knows all the typical imperative controlflow sentences and supports mutable variables and attributes; Simultaneously, Swift also supports typical functional programming features, such as value types and first-class features.

In Swift, we often have the choice: do we wish to express a logical expression in an imperative or functional way?

As a simple example, we summarize the total width of a number of views:

  was width: CGFloat = 0
for display in captions {
width + = view.frame.width
}

This is simply an important way of expressing this logic. However, we could also choose the functional option:

  la width = subviews.reduce (0) {result, view in
result + view.frame.width
}

Both snippets do the same, with a slight difference: in the absolute case, we are left with a variable width variable after the calculation is done while we have an unalterable width variable in the functional case.

To make the code a bit more interesting, we will try to summarize the total width of the views where is hidden is false . When the logic becomes more complex, the alternative ways begin to express it to diverge.

Here is the absolute version:

  was width: CGFloat = 0
for display in captions {
guard! view.isHidden else {continue}
width + = view.frame.width
}

Of course, there are other ways to express this logic, for example, using a where clause:

  for viewing in the whereabouts where! View.isHidden {
width + = view.frame.width
}

Let's try a functional version that achieves the same. An alternative is to first filter out the hidden views and then use reduce to calculate total width:

  la visibleViews = subviews.filter {! $ 0.isHidden}
let the width = visibleViews.reduce (0) {result, view in
result + view.frame.width
}

Alternatively we may include the isHidden check in the reduction function:

  la width = subviews.reduce (0) {result, view in
view.isHidden? result: result + view.frame.width
}

A nice feature of the previous version ̵

1; first filtering, and then decreasing – is that reduces the portion of the logic is the same, regardless of whether we summarize the width of all views, just the same views or other parts by them. This compilation is a clear suggestion that the logic can be abstracted, for example, using a calculated property:

  extension Array der Element == UIView {
were total Width: CGF
return reduce (0) {result, display in result + view.frame.width}
}
}

Now the total width of the regular views is simple:

  leave the width = visibleViews.totalWidth

Once again, the implementation of this extension and the recovery of visible vision could also be imperative:

  extension Array der Element == UIView {
were total Width: CGF
were results: CGFloat = 0
for viewing by yourself {
result + = view.frame.width
}
return result
}
}

were visibleViews: [UIView] = []
for viewing in shows where! view.isHidden {
visibleViews.append (view)
}
la width = evenViews.totalWidth

Swift allows mixing and customization of important and functional programming techniques, and often what technique you prefer is a matter of taste. We use both in our own code; For some issues, we use maps or for other problems, we write an important loop and summarize the results as we go.

Whichever you choose, break the problem into smaller parts, as we've done with the extension above TotalWidth above, is a very useful technique for both.

To learn more about functional approaches in Swift, our book Functional Swift is a great place to start 🙂


Source link