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

标签 ios swift macos

“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/

相关文章:

ios - 带有多个图像的自定义 MKAnnotationView

ios - 用户名 RegEx Swift

swift 调度队列 : Serial or parallel

objective-c - 如何在没有 facebook Kit 的情况下在 OSX 应用程序中在 facebook 上分享?

ios - 在 Swift 中从 AppDelegate 加载嵌入到 UINavigationController 的 Controller

iphone - iOS 应用商店审查中的分析软件用户内容(应用被拒绝)

swift - 尝试使用函数从 Firebase 存储返回 downloadUrl

swift - ARKit ARSCNView.snapshot() 内存泄漏

macos - 为什么 fatfree 在 Mac 上需要 RewriteBase 而不是 Ubuntu?

macos - Objective-C 将图像拖入 NSTextView 子类