gpt4 book ai didi

string - “字符串”无法转换为 '[(String)]'

转载 作者:行者123 更新时间:2023-11-30 10:20:12 24 4
gpt4 key购买 nike

我试图用“,”分割从 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”无法转换为 '[(String)]'”

有人知道如何正确地将字符串拆分到数组中吗?

最佳答案

有问题的行在转换中缺少[] - 当您需要转换为数组时,您正在转换为String 字符串:

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
}

关于string - “字符串”无法转换为 '[(String)]',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27199240/

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