【问题标题】:Append array of arrays Swift追加数组数组 Swift
【发布时间】:2015-09-06 18:26:56
【问题描述】:

我有一个名为 Lines 的数组数组

Numbers = [[1,2,3],[4,5,6],[7,8,9]]

var numberInt = 10

我如何将这个numberInt 附加到多维数组的最后一个数组中?所以它看起来像

Numbers = [[1,2,3],[4,5,6],[7,8,9,10]]

谢谢。

【问题讨论】:

  • 您似乎没有尝试过任何东西。你应该(特别是因为这实际上是微不足道的。)
  • 如何检索数组中的最后一个元素并将数字附加到检索到的数组中

标签: ios arrays swift


【解决方案1】:

使用Numbers[Numbers.count - 1]获取最后一个一维数组,使用append方法添加item:

Numbers[Numbers.count - 1].append(numberInt)
print(Numbers) // prints "[[1, 2, 3], [4, 5, 6], [7, 8, 9, 10]]"

我还建议你看看这个答案:https://stackoverflow.com/a/25128237/1796907

【讨论】:

  • @TheParamagneticCroissant Numbers.last 是只读属性,所以我不能使用 append
  • @TheParamagneticCroissant 所以不是那么简单,对吧?)
  • 感谢您的回答。像魅力一样工作。我会研究链接
  • @DavidRobertson “不是那么简单”——为什么不呢?
  • 我和羊角面包在一起。这是一个小问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-01
  • 2016-05-05
  • 2014-11-03
  • 1970-01-01
  • 1970-01-01
  • 2015-06-04
  • 1970-01-01
相关资源
最近更新 更多