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)")
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,count()
function has been replaced by a "protocol extension" method count()
.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With