I get an error I cannot explain when trying to call the count()
function in a switch case with enums:
enum Token{
case Name(String)
var count:Int{
switch self{
case .Name(let string):
return count(string)
}
}
}
The error is Cannot invoke 'count' with an argument list of type '((String))'
.
I also tried
case .Name(let string):
return string.characters.count
with the error 'String' does not have a member named 'characters'
.
Anybody an idea what I`m doing wrong?
Swift thinks you are trying to access the count
property instead of calling the global count
function. You can fix this by calling Swift.count(string)
:
Swift 1.2:
enum Token{
case Name(String)
var count:Int{
switch self{
case .Name(let string):
return Swift.count(string)
}
}
}
For Swift 2:
The second syntax you tried is actually correct for Swift 2:
case .Name(let string):
return string.characters.count
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.