swift swift中字符的简单索引表

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift swift中字符的简单索引表相关的知识,希望对你有一定的参考价值。

//sample holding table for indexes of characters
struct IndexTable {
    let string:String
    //table of strings mapping to their indexes in the text
    var indexes:[Character:Set<Int>]
    
    //forms the indexed character table
    init(string:String){
        self.string = string
        self.indexes = [Character:Set<Int>]()
        let chars = Array(string.characters)
        for i in 0..<chars.count {
            //needs to mutate the entry in the dictionary
            if let _ = self.indexes[chars[i]] {
                self.indexes[chars[i]]!.insert(i)
            }
            else {
                self.indexes[chars[i]] = Set<Int>()
                self.indexes[chars[i]]!.insert(i)
            }
        }
    }
}

以上是关于swift swift中字符的简单索引表的主要内容,如果未能解决你的问题,请参考以下文章

Swift 3 - 如何从包含字符串的索引中读取 json 输出

Swift 2 中的字符串索引

在 Swift 的嵌套循环中从特定索引枚举字符串

Swift 2 字符串数组删除特定索引错误

Swift:获取String中单词的开头和结尾字符的索引

如何在swift中将字符索引从layoutManager转换为String scale