Igor Tupitsyn
Igor Tupitsyn

Reputation: 1193

Swift array.map - cannot invoke 'map'

I am using the following code in xCode 6.4 to split strings inside an array into arrays:

func getData() -> [String]
{
    let data = navData

    // navData is like:
    // A|xxx|xxx|xxx
    // R|ccc|ccc|ccc
    // N|ccc|ccc|ccc
    // N|ccc|ccc|ccc

    return split(data) { $0 == "\n" }
}

let data:[String] = getData()

func search(query:(String, Int)) -> [String]
{
    let recs:[String] = data.filter { $0.hasPrefix(query.0) }
    var cols: [String] = recs.map { split( recs ) { $0 == "|" } }
}

func searchQueries() -> [(String, Int)]
{
    return [("N", 1)] //key, column index
}


for q:(String, Int) in searchQueries()
{
    var results:[String] = search(q)
    for x in results
    {
        result = results[0]
    }
 }

It used to work before, but I guess swift was changed in 1.2 and it gives the following error now:

Cannot invoke 'map' with an argument list of type '(() -> _)'

Any suggestions?

Thank you!

Upvotes: 0

Views: 404

Answers (1)

Mundi
Mundi

Reputation: 80265

After discovering that in Swift two you have to split strings by using its characters property, I made this work in playground:

let recs = ["col1|col2|col3", "1|2|3"]
let cols = recs.map {
    split($0.characters) { $0 == "|" }.map {String($0)}
}
cols.first // ["col1", "col2", "col3"]
cols.last  // ["1", "2", "3"]

Note that in Swift 2 beta 2 you can also use {String.init} at the end.

To make this work in Swift 1.2, remove .characters.

Upvotes: 1

Related Questions