简体   繁体   中英

type “string” doesn't conform to protocol “sequence”

Why does it come to this when I try to print every char in string a?

import Foundation


let a = "what is this"
for b in a {
    print(b)
}

在此处输入图片说明

A String is not a sequence, you need to call the characters property to get a sequence .

let a = "what is this"
for b in a.characters {
  print(b, terminator: "")
}

// "what is this"

You have to use the characters member variable of String if you want to enumerate all the characters.

let a = "what is this"
for b in a.characters 
{
   print(b)
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM