Higher order function: "Cannot invoke map using a list of arguments of type ((_) & # 8594; _)"

I would like to use a faster function of a higher order (map) to remove all Subviews from the given array of UIView.subviews. Line

(cell.contentView.subviews as [UIView]).map { $0.removeFromSuperView() } 

throws an error "Cannot call" map "using an argument list of type '((_) โ†’ _)'"

I would like to know what compiler I need at the moment.

+5
source share
1 answer

I would say that the card is not for this kind of operation. It creates a new sequence based on other elements of the sequence, but that you do not want to create a sequence, you just want to iterate through them and apply a function to them. In fast, there is no higher order function that matches what you want, I hope they put something soon. Therefore, it is best to use a for loop or write your own function that does what you want.

I would suggest writing your own functionality (based on what the foreach scalar is):

 extension Array { func foreach(function: T -> ()) { for elem in self { function(elem) } } } 

UPDATED with Swift 2.0

forEach added to SequenceType , so it is available:

 (cell.contentView.subviews as [UIView]).forEach { $0.removeFromSuperview() } 
+22
source

Source: https://habr.com/ru/post/1213911/


All Articles