Swift:如何将String转换为[String]?

时间:2016-01-13 19:34:56

标签: string swift character

我需要将经典String转换为[String](将每个字符分开)。

// let alphabet1 = ["a", "b",...
// let nombres1 = [1, 2,...
var name1 : String = "Hi"
let namet1 : [String] = name1.characters.map{String($0)}
func namepoints(name : [String], alphabet : [String], nombres : [Int]) -> Double {
    func nestedCountvalue(tableau : [String]) -> Int{
        var a : Int = 0
        for _ in tableau {
            a++
        }
        return a
    }
    var moy : Double = 0
    var p : Int = 0
    var i : Int = 0
    var o : Int = 0
    var pts : Int = 0
    p = nestedCountvalue(name)
    while i <= p{
        if name[i] == alphabet[o]{ // EXC_BAD_INSTRUCTION (Array index out of range)
            pts += nombres[o]
            o = 0
            i++
        } else {
            o++
        }
        if i == p {
            print("\(pts) points.")
            moy = Double(pts) / Double(p)
            print("\(moy) points en moyenne.")
            return moy
        }
    }
    return 0
}

0 个答案:

没有答案