gpt4 book ai didi

ios - "for char in self"有一个错误说 "type string does not conform to protocol sequencetype"

转载 作者:搜寻专家 更新时间:2023-11-01 07:22:43 25 4
gpt4 key购买 nike

“for char in self”有一个错误说“type string does not conform to protocol sequencetype”

我做了与教程完全相同的事情,但教程中没有显示错误。

这个错误是关于 Swift 的版本吗?

如何解决?

谢谢

import UIKit
import Darwin

extension String{
var asArray:[Character]{
var result = [Character]()
for char in self{
result.append(char)
}
return result
}
}

var str = "a random string"
print(str.asArray)

最佳答案

如果你想在 Swift 2.2 中创建一个 Character(s) 数组,你可以使用下面的代码

let text = "Hello World"
let chars = Array(text.characters)
print(chars) // ["H", "e", "l", "l", "o", " ", "W", "o", "r", "l", "d"]

关于ios - "for char in self"有一个错误说 "type string does not conform to protocol sequencetype",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37891275/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com