Yossi Tsafar Yossi Tsafar -5 years ago 333
Swift Question

Type 'String' does not conform to protocol 'SequenceType' - Swift 2.0

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 = ""
for character in string {
buffer.insert(character, atIndex: buffer.startIndex)

return buffer

The error:

Type 'String' does not conform to protocol 'SequenceType'

Answer Source

Easy solution:

func reverseString(string: String) -> String {
  return String(string.characters.reverse())

Your code works with this change

for character in string.characters {


In Swift 3 reverse() is replaced with reversed()

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download