Programming in Swift · Challenge: Closures | Ray Wenderlich


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/5994-programming-in-swift/lessons/36

Not sure if this is the right place to ask but: in the first exercise about reducing the array of names to a single string, I wrote the closure in the shorter syntax:

let concatNames = names.reduce("") { $0 + $1 }

When I do this, the output from the Swift playground next to this line says (6 times) and doesn’t print the result, whereas in your solution (with more verbose syntax) it says (5 times) and prints the result. Any idea why?

Hi! If you keep your short syntax, but spread it out over multiple lines like this:

let concatNames = names.reduce("") {
  $0 + $1
}

You should see the (5 times) as expected. The reverse is also true! If you smashed our long syntax version onto one long time, you’d see (6 times) in the sidebar.

I believe this has to do with the way that playgrounds displays feedback in the sidebar. (reduce is executing 6 times, but the closure only 5). Another way to compare output is to look at the value history using the “Show Result” button to the right of each bit of output.

Thanks for the reply!

06
Hey, how this filter closure connected with .reduce and how they work together at the end ??? Didn’t get it

@didar490 You use filter( _: ) to get the names that have more than five characters from names and chain the call with reduce( _: _: ) to build a custom string from the filtered names.

Please let me know if you have any other questions or issues about the whole thing. Thank you!

Hi Catie,

I was wondering how you could concatenate the first exercise in a for loop. I have tried it but it gives me an error.

Here’s my code:

let names = [“Ray”, “Vicky”, “Ozma”, “Catie”, “Jessy”]

var allnames: [String] = []

for concatNames in names {

allnames += concatNames

}

It gives me the following error: Binary operator ‘+=(::)’ cannot be applied to operands of type ‘[String]’ and ‘String’

Option clicking on concatNames shows that it’s a String. How can I turn it into an array?

My thinking is probably way off. But i’m still having trouble with types :confused:

Just started this course which is really amazing btw :blush: Really really satisfied

1 Like

Hi!

If you want to concatenate all of the names into a single string, then you don’t actually want to turn concatNames into an array. You want the type of allNames to be String instead of [String].

So your code would look like this (I’ve done a bit of renaming here):

let names = ["Ray", "Vicki", "Ozma", "Catie", "Jessy"]

var allNames: String = "" //You don't have to explicitly type this variable, I've just done it for clarity

for name in names { 
  allNames += name
}

But since you asked, to create a String Array from a String, simply wrap it in square brackets. To use your exact example:

allnames += [concatNames]

But of course. Allnames wasn’t supposed to be an Array but a String. Thank you so much for your help Catie! Really apreciate it :blush:

1 Like