Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift compiler error: “Cannot invoke 'map' with an argument list of type '((_) -> _)'”

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?

like image 884
tkuichooseyou Avatar asked Apr 29 '15 20:04

tkuichooseyou


1 Answers

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.

like image 114
Sulthan Avatar answered Nov 12 '22 15:11

Sulthan