I\'m trying to reverse string in Swift 2.0 but I get an error on the string ifself.
func reverseString(string: String) -> String {
var buffer = \"\"
Easy solution:
func reverseString(string: String) -> String {
return String(string.characters.reverse())
}
Your code works with this change
for character in string.characters {
Swift 3:
In Swift 3 reverse()
has been renamed to reversed()
Swift 4:
In Swift 4 characters
can be omitted because String
returned to behave like a sequence.
func reverseString(string: String) -> String {
return String(string.reversed())
}
As of Swift 2, String
doesn't conform to SequenceType
.
You can add an extension.
extension String: SequenceType {}