我正在从解析中检索多个数组并将它们存储在一个数组中。它目前正在工作,但是当我在 [indexpath.row]
应用文本时,标签是 ["cat", "dog"]
而我想要 cat,狗
。
var animal: [[String]] = []
if let displayIntake = object["Animal"] as? [String]{
self.animal.append(displayIntake)
print(self.animal)
//prints ["cat", "dog"]
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell: TutorBoxCell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! BoxCell
cell.info.text = "\(animal[indexPath.item])"
//info is a UILabel
//on the app the label appears ["cat", "dog"] instead I need it to be cat, dog
最佳答案
您可以在数组上使用joinWithSeparator
。
它将把数组元素连接为字符串,并与作为参数传递的字符串分隔。
示例:
yourArray.joinWithSeparator(", ")
给出
cat, dog
如果它是数组的数组,请使用这个:
yourArray.map { $0.joinWithSeparator(", ") }.joinWithSeparator("")
这意味着我们用“,”连接每个子数组,然后将所有内容连接到一个字符串中。
考虑到您在评论中向我展示的数组内容,适合您的组合将类似于以下示例:
let animals = [ ["dog", "cat"], ["chicken", "bat"] ]
let results = animals.map { $0.joinWithSeparator(", ") }
for content in results {
print(content)
}
打印
dog, cat
chicken, bat
为了使示例完整:
let all = results.joinWithSeparator(" - ")
print(all)
打印
dog, cat - chicken, bat
关于arrays - 如何删除数组数组中的 [""]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34999246/