0

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!

2
  • @Mundi I think we can assume that data looks something like ["abc|def", "ghi|jkl"]. Commented Jul 16, 2015 at 23:14
  • @Aaron Brager. Yes, after data.filter it looks exactly as you said: [N|ccc|ccc|ccc,N|ccc|ccc|ccc] Commented Jul 16, 2015 at 23:18

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, Mundi. Your code does not work. I suspect due to its lack of compatibility with swift 1.2... Thanks, anyways! Would need it when v2 is officially out.
It works if you remove .characters: split($0) { $0 == "|" }.map {String($0)}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.