Swift数组仅打印最后一个元素

时间:2019-08-06 04:35:14

标签: arrays swift realm

这是我的课程。

class dictionaryWord Object
{
    @objc dynamic var word: String?
    @objc dynamic var Defination: String?
    @objc dynamic var dateCreated: Date?  
}

我想做的是我的定义太长,因此我想用逗号将其分开,并将文本显示为新行。我已打印所有元素的代码 控制台中的数组,但仅在文本标签上显示最后一个元素。谢谢。

let definationSplit = "\(String(describing: (wordOfDay.Defination!)))"
let completedSplit: [String] = definationSplit.components(separatedBy: ",")

word.text = "\(String(describing: (wordOfDay.word!)))"

for (index,element) in completedSplit.enumerated() {
    wordDescription.text = "\(index),\(element)"
    print (index,"\u{2022}",element)
}

1 个答案:

答案 0 :(得分:1)

如果需要标签具有数组中的所有元素,请将这些元素附加到字符串中。然后使用此字符串设置标签文本。

var labelText = ""
for (index,element) in completedSplit.enumerated() {
    labelText += "\(index),\(element)"
    print (index,"\u{2022}",element)
}
wordDescription.text = labelText