我从解析中检索多个数组并将它们存储在一个数组中。它当前正在工作,但是当我在[indexpath.row]
标签上应用文本时,["cat", "dog"]
我却想要cat, dog
。
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(", ")
给
猫狗
如果它是一个数组数组,请使用它:
yourArray.map { $0.joinWithSeparator(", ") }.joinWithSeparator("")
这意味着我们用“,”连接每个子数组,然后将所有内容都连接到一个字符串中。
考虑到您在注释中显示的数组内容,适合您的组合将类似于以下示例:
let animals = [ ["dog", "cat"], ["chicken", "bat"] ]
let results = animals.map { $0.joinWithSeparator(", ") }
for content in results {
print(content)
}
印刷
狗,猫,
鸡,蝙蝠
为了使示例更完整:
let all = results.joinWithSeparator(" - ")
print(all)
印刷
狗,猫-鸡,蝙蝠
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句