'字符串"不能转换为' [(String)]'

时间:2014-11-29 05:35:34

标签: string swift

我试图通过","分割从URL返回的字符串。但是当我尝试

var tagArray = [String]()
if response.responseObject != nil {
                let data = response.responseObject as NSData
                let str = NSString(data: data, encoding: NSUTF8StringEncoding)!
                self.tagArray = str.componentsSeparatedByString(", ") as String
                println("response: \(self.tagArray)") //prints the HTML of the page
            }

self.tagArray = str.componentsSeparatedByString(", ") as String行会引发错误"' String"不能转换为' [(字符串)]'"

任何人都知道如何正确地将字符串拆分成数组?

1 个答案:

答案 0 :(得分:1)

违规行缺少广告投放中的[] - 当您需要投射到{em>数组 String时,您需要投放到Strings {1}}:

var tagArray = [String]()
if response.responseObject != nil {
    let data = response.responseObject as NSData
    let str = NSString(data: data, encoding: NSUTF8StringEncoding)!
    self.tagArray = str.componentsSeparatedByString(", ") as [String]
    println("response: \(self.tagArray)") //prints the HTML of the page
}