Raphael
Raphael

Reputation: 8192

Swift - Map to different type

Suppose that I have a text and an array of NSRanges. From that, I want to produce an array of substrings. What I would like to do is to use a simple map function and get done with it:

let substrings = ranges.map { return (text as NSString).substringWithRange($0) }

Swift won't actually compile this as it expects that the return type of the function to be applied by the map to be the same as the array's type. Is there any way to map those values to a different type?

Upvotes: 0

Views: 3787

Answers (1)

keithbhunter
keithbhunter

Reputation: 12324

The map closure does not need a return statement.

let substrings = ranges.map { (text as NSString).substringWithRange($0) }

EDIT: The return statement appears to be implicit if you leave it out. So your code should work as it is. What is your error?

Upvotes: 3

Related Questions