Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String does not have a member named characters?

Tags:

ios

swift

I was trying the Swift playground. When I tried the code below, it did not work and told me 'String' does not have a member named 'Characters'. I expect to print the number of characters in cafe is 4. Could you give me any tips? Thanks.

var word = "cafe"
print("the number of characters in \(word) is \(word.characters.count)")
like image 816
David Avatar asked Jun 20 '15 22:06

David


1 Answers

characters is a property of String in the "new" Swift 2 that comes with Xcode 7 beta.

You are probably using Xcode 6.3.2 with Swift 1.2, then it is

print("the number of characters in \(word) is \(count(word))")

Two things changed with Swift 2.0 here:

  • String does no longer conform to SequenceType, you have to access .characters explicitly,
  • The global count() function has been replaced by a "protocol extension" method count().
like image 71
Martin R Avatar answered Nov 15 '22 23:11

Martin R