我正在尝试在一个打印语句中打印元素,例如[1,2,3,4,5]但没有围绕数据外部的括号。这可能吗?
答案 0 :(得分:1)
试试这个 -
let arrayName = ["1","2","3","4","5"]
for element in arrayName {
print(element, terminator: " ") // output 1 2 3 4 5
}
回答你的第二个问题 -
var string: String?
for element in arrayName {
if string == nil {
string = element
} else {
string = string! + ", " + element
}
}
print(string!) // output - 1, 2, 3, 4, 5
答案 1 :(得分:0)
extension Array {
var toPrint: String {
var str = ""
for element in self {
str += "\(element) "
}
return str
}
}
let arrayName = ["1","2","3","4","5"]
print(arrayName.toPrint) // output: 1 2 3 4 5
基于Prema Janoti回答https://stackoverflow.com/a/42297499/7064692