Swift 1.2 cannot invoke 'count' with an argument list of type '(String)'
Asked Answered
S

5

7

Updated to Xcode 6.3.1 with new Swift 1.2, the old method countElement change to count, however when I switch to use count, it always throw out this error message:

cannot invoke 'count' with an argument list of type '(String)'

This snippet is I copied from Apple doc, but still not working.

func printAndCount(stringToPrint: String) -> Int {
    println(stringToPrint)
    return count(stringToPrint)
}

func printWithoutCounting(stringToPrint: String) {
    printAndCount(stringToPrint)
}

printAndCount("hello, world")
Samaria answered 24/4, 2015 at 18:56 Comment(2)
That code compiles and runs without problems in my Xcode 6.3.1.Charlie
possible duplicate of String length in Swift 1.2 and Swift 2.0Susy
C
4

Try calling the global count function with the Swift module prefix like this:

Swift.count(stringToPoint)

E.g. when extending the Array type there is a property named count as well and thus there is a naming conflict.

Caucasoid answered 2/6, 2015 at 8:36 Comment(1)
I'm not sure if this is a bug, but adding to all my 'count' calls the Swift module prefix did solve this problem! Great tip Klaas, you saved my day!Glomeration
C
3

Did you declare the variable "count" in the same scope? If so, you should change the variable name. And try to build again.

Collectanea answered 25/5, 2015 at 2:50 Comment(2)
Seems like a bug to me.Valene
Doh. Not a bug since the scope overriding is behaving as expected, but it's an easy mistake to make, and the error message is not helpful.Evangelina
R
2

I had the same problem and it was making me crazy.

To solve it, I used:

 myString.characters.count

Bingo.

Rockribbed answered 12/10, 2015 at 4:36 Comment(1)
I also had the same problem and prefixing Swift didn't work for me. But your answer actually worked. No idea how this bug came aboutAvidity
F
-1

Have you tried a clean and build? It works here.

Ferromanganese answered 24/4, 2015 at 19:12 Comment(2)
Well, I create a new playground and the code works fine. I guess it's because the previous playground swift version messed up.Samaria
Sometimes you just need to reload it (by quitting Xcode or cutting and pasting code).Ferromanganese
S
-1

Well, I create a new playground and copy the code works fine. I guess it's because the previous playground swift version messed up. Create a new project works fine.

Samaria answered 24/4, 2015 at 21:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.