goldenlimit
goldenlimit

Reputation: 92

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

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")

Upvotes: 7

Views: 4150

Answers (5)

Evan K. Stone
Evan K. Stone

Reputation: 1199

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

To solve it, I used:

 myString.characters.count

Bingo.

Upvotes: 2

Klaas
Klaas

Reputation: 22773

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.

Upvotes: 4

Reo Yoshida
Reo Yoshida

Reputation: 137

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

Upvotes: 3

goldenlimit
goldenlimit

Reputation: 92

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.

Upvotes: -1

Richard Birkett
Richard Birkett

Reputation: 781

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

Upvotes: -1

Related Questions