9

I'd like to remove all subviews of my label like so:

label.subviews.map { $0.removeFromSuperview() }

This used to work in Xcode 6. But in Xcode 7 I get the following warning:

Result of call to 'map' is unused

This warning doesn't make sense. How can I suppress it?

UPDATE: I got this to work by doing the following, however, I'd like to keep a functional programming style and stick with map { }. This should also work.

label.subviews.forEach { $0.removeFromSuperview() }
Daniel
  • 3,758
  • 3
  • 22
  • 43
  • 1
    You could use the foreach extension described [here](http://stackoverflow.com/a/24446427/730701). – Adam Sep 17 '15 at 06:51
  • Thanks! However, I'd still like to have map { } work in order to maintain functional programming style. – Daniel Sep 17 '15 at 07:25
  • 5
    @Daniel `forEach` has functional programming style as well. – Dániel Nagy Sep 17 '15 at 07:26
  • @DánielNagy: I would not consider it a duplicate of *that* question. That question is about a different error message (caused by a typo), this question is about a warning (unused result). Even if the same answer applies, the questions are different. – Martin R Sep 17 '15 at 07:28
  • Thanks, Daniel Nagy, that is exactly the response (with explanation!) I was looking for. It's not exactly the same, but answered my question. Koszi – Daniel Sep 17 '15 at 07:28
  • @Daniel I'm glad I could help! Szívesen :) – Dániel Nagy Sep 17 '15 at 07:53
  • 6
    You are trying to use map as a general purpose looping construct which is not the idea of map. In FP map means convert a set of value to other set of values which is not what your requirement of "removing a set of subviews" is about – Ankur Sep 17 '15 at 07:59
  • @Ankur, you are converting. You can think of it as converting to nil. (Or using a method to convert each element, in this case remove(), which does something, somehow. It happens to remove, but map doesn't care about that.) I don't think that's the problem. The problem is that the list is immutable and map { } simply creates a new list as opposed to modifying the actual label.subviews list. – Daniel Sep 17 '15 at 12:35
  • @Daniel: That something that you are doing in the map is not referential transparent though so it is not really a FP function – Ankur Sep 17 '15 at 13:18
  • 6
    @Daniel I don't see how you can both want to "keep a functional programming style" and consider the immutability a problem. `map` is simply the wrong tool for the job here, and `forEach` was created exactly for this purpose. – molbdnilo Sep 17 '15 at 13:22
  • 2
    _ = label.subviews.map { $0.removeFromSuperview() } – valfer Nov 06 '15 at 10:46

0 Answers0