【问题标题】:Insert element into [(String, String)] tuple in swift [closed]在swift中将元素插入[(String,String)]元组[关闭]
【发布时间】:2016-08-24 13:39:11
【问题描述】:

我正在使用 WKInterfacePicker,并且需要将动态数据插入格式为 [(String, String)] 的数组中。我可以像这样将数据静态插入到这个数组中,

var cc:[(String, String)] = [
("Item 1", "Row 1"),
("Item 2", "Row 2")]

谁能帮我如何动态插入数据?

【问题讨论】:

  • 它是 (String,String) 的数组。我无法将字符串附加到数组,然后将数组附加到它...
  • @EICaptainv2.0 这是一个元组数组

标签: ios swift watchkit


【解决方案1】:
var myArray = [(String, String)]()

for index in 1...20 {
    myArray.append(("Item \(index)", "Row \(index)"))
}

print(myArray) // "[("Item 1", "Row 1"), ("Item 2", "Row 2"), ("Item 3", "Row 3"), ("Item 4", "Row 4"), ("Item 5", "Row 5"), ("Item 6", "Row 6"), ("Item 7", "Row 7"), ("Item 8", "Row 8"), ("Item 9", "Row 9"), ("Item 10", "Row 10"), ("Item 11", "Row 11"), ("Item 12", "Row 12"), ("Item 13", "Row 13"), ("Item 14", "Row 14"), ("Item 15", "Row 15"), ("Item 16", "Row 16"), ("Item 17", "Row 17"), ("Item 18", "Row 18"), ("Item 19", "Row 19"), ("Item 20", "Row 20")]\n"

【讨论】:

  • 感谢您的回复,但如何动态提供 tuple1 值?
  • 动态是什么意思?数据从何而来?
  • 我期待的数据是一个for循环整数1到20转换成字符串
  • @PradheepNarendranP 是您要找的吗?
  • 我正在寻找您在回答中提到的元组插入。我搞定了!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-10-29
  • 1970-01-01
  • 1970-01-01
  • 2019-08-19
  • 1970-01-01
  • 2013-05-27
  • 2020-06-28
相关资源
最近更新 更多