【问题标题】:type "string" doesn't conform to protocol "sequence"类型“字符串”不符合协议“序列”
【发布时间】:2016-12-23 00:28:52
【问题描述】:

当我尝试打印字符串 a 中的每个字符时,为什么会出现这种情况?

import Foundation


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

【问题讨论】:

    标签: swift string sequence


    【解决方案1】:

    String 不是序列,您需要调用characters 属性来获取sequence。

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

    【讨论】:

      【解决方案2】:

      如果需要,您必须使用String 的characters 成员变量 枚举所有字符。

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

      【讨论】:

        猜你喜欢
        • 2016-10-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-06-22
        • 2017-02-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多