I have a range that I'm trying to map on, but I'm getting the error
“Cannot invoke 'map' with an argument list of type '((_) -> _)'”
Here's what the code looks like
let patterns = (0...5).map { verseNum in
let verseNumberStartPattern = "\"verse-num\">\(verseNum)</span>(?:\\s?<span>)?(.*?)<"
let chapterStartPattern = "\"chapter-num\">\\s\(parsedVerse.chapterStart)\\s</span>"
if verseNum == 1 {
return chapterStartPattern + "(.*?)<"
} else {
return chapterStartPattern + "(?:.*?)" + verseNumberStartPattern
}
}
If I take out everything inside the closure and just return "", then the compiler doesn't complain. However, even if I add one line other than the return empty string, then the compiler complains, such as for:
let patterns = (0...5).map { verseNum in
let verseNumberStartPattern = "\"verse-num\">\(verseNum)</span>(?:\\s?<span>)?(.*?)<"
return ""
}
Am I missing something here?
Swift can't infer types from the context every time. If it can't infer types, you have to type them explicitly, in this case the return type:
let patterns = (0...5).map { verseNum -> String in
In this case I believe Swift should be able to infer the type so it might be a bug.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With