本文翻译自:How do I concatenate or merge arrays in Swift?

If there are two arrays created in swift like this: 如果有两个这样迅速创建的数组:

var a:[CGFloat] = [1, 2, 3]
var b:[CGFloat] = [4, 5, 6]

How can they be merged to [1, 2, 3, 4, 5, 6] ? 如何将它们合并到[1, 2, 3, 4, 5, 6]


#1楼

参考:https://stackoom.com/question/1hVio/如何在Swift中串联或合并数组


#2楼

You can concatenate the arrays with + , building a new array 您可以使用+连接数组,以构建新数组

let c = a + b
print(c) // [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]

or append one array to the other with += (or append ): 或使用+=将一个数组附加到另​​一个数组(或append ):

a += b// Or:
a.append(contentsOf: b)  // Swift 3
a.appendContentsOf(b)    // Swift 2
a.extend(b)              // Swift 1.2print(a) // [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]

#3楼

If you are not a big fan of operator overloading, or just more of a functional type: 如果您不是运算符重载的忠实拥护者,或者不是更多的功能类型:

// use flatMap
let result = [["merge", "me"], ["We", "shall", "unite"],["magic"]
].flatMap { $0 }
// Output: ["merge", "me", "We", "shall", "unite", "magic"]// ... or reduce
[[1],[2],[3]].reduce([], +)
// Output: [1, 2, 3]

#4楼

My favorite method since Swift 2.0 is flatten 自从Swift 2.0 展平以来,我最喜欢的方法

var a:[CGFloat] = [1, 2, 3]
var b:[CGFloat] = [4, 5, 6]let c = [a, b].flatten()

This will return FlattenBidirectionalCollection so if you just want a CollectionType this will be enough and you will have lazy evaluation for free. 这将返回FlattenBidirectionalCollection因此,如果您只想要CollectionType就足够了,并且可以免费进行懒惰评估。 If you need exactly the Array you can do this: 如果您确实需要数组,可以执行以下操作:

let c = Array([a, b].flatten())

#5楼

To complete the list of possible alternatives, reduce could be used to implement the behavior of flatten : 为了完成可能的选择列表,可以使用reduce来实现flatten的行为:

var a = ["a", "b", "c"]
var b = ["d", "e", "f"]let res = [a, b].reduce([],combine:+)

The best alternative (performance/memory-wise) among the ones presented is simply flatten , that just wrap the original arrays lazily without creating a new array structure. 所提出的最佳选择(性能/内存方面)仅是flatten ,它只是懒散地包装原始数组,而无需创建新的数组结构。

But notice that flatten does not return a LazyCollection , so that lazy behavior will not be propagated to the next operation along the chain (map, flatMap, filter, etc...). 但是请注意, flatten 不会返回 LazyCollection ,因此,懒惰行为不会沿链(地图,flatMap,过滤器等)传播到下一个操作。

If lazyness makes sense in your particular case, just remember to prepend or append a .lazy to flatten() , for example, modifying Tomasz sample this way: 如果懒惰在您的特定情况下有意义,则只需记住将.lazy前缀或附加到flatten() ,例如,以这种方式修改Tomasz示例:

let c = [a, b].lazy.flatten()

#6楼

If you want the second array to be inserted after a particular index you can do this (as of Swift 2.2): 如果要在特定索引之后插入第二个数组,可以执行以下操作(从Swift 2.2开始):

let index = 1
if 0 ... a.count ~= index {a[index..<index] = b[0..<b.count]
}
print(a) // [1.0, 4.0, 5.0, 6.0, 2.0, 3.0]

如何在Swift中串联或合并数组?相关推荐

  1. swift java混合,如何在Swift中连接或合并数组?

    使用Swift 3,根据您的需求和品味,您可以选择其中一个 five following ways 来连接/合并两个数组 . 1.使用Swift标准库(: :)泛型运算符将两个数组合并为一个新数组 S ...

  2. swift语言 数组定义_如何在Swift中声明弱引用数组?

    I'd like to store an array of weak references in Swift. The array itself should not be a weak refere ...

  3. 如何在Python中串联两个列表?

    如何在Python中串联两个列表? 例: listone = [1, 2, 3] listtwo = [4, 5, 6] 预期结果: >>> joinedlist [1, 2, 3, ...

  4. 浅层学习与深层学习_深层副本与浅层副本-以及如何在Swift中使用它们

    浅层学习与深层学习 by Payal Gupta 通过Payal Gupta 深层副本与浅层副本-以及如何在Swift中使用它们 (Deep copy vs. shallow copy - and h ...

  5. 如何在Swift中创建漂亮的iOS图表

    通过图形和图表呈现数据是当今移动应用程序最显着的特征之一.iOS图表使应用程序看起来更漂亮,更有吸引力. 在本教程中,我们将向您展示如何使用代码示例在Swift中实现我们的iOS图表.我们将看一下Sw ...

  6. python中2d_【IT专家】如何在Python中复制一个2D数组?(复制)

    本文由我司收集整编,推荐下载,如有疑问,请与我司联系 如何在 Python 中复制一个 2D 数组? ( 复制 ) 如何在 Python 中复制一个 2D 数组 ?( 复制 )[ 英 ]How to  ...

  7. 如何在Swift中使用CoreData设置有用的自动完成UITextField

    by Emrick Sinitambirivoutin 由Emrick Sinitambirivoutin 如何在Swift中使用CoreData设置有用的自动完成UITextField (How t ...

  8. 如何在Swift中发出HTTP请求?

    本文翻译自:How to make an HTTP request in Swift? I read The Programming Language Swift by Apple in iBooks ...

  9. 如何在 Swift 中进行错误处理

    作者:Olivier Halligon,原文链接,原文日期:2015-12-17 译者:JackAlan:校对:靛青K:定稿:Channe 今天的文章讲解如何在 Swift 中进行错误处理. 说实话, ...

最新文章

  1. 求不同字母全排列两种递归模板
  2. DM368学习--捕获视频图像分辨率修改
  3. ERROR: Resource shrinker cannot be used for libraries报错Android开发之迁移老项目到Android Studio3.0报错的问题解决方法
  4. c语言扩展运算符是什么,扩展运算符(spread)是三个点(…)
  5. Ideal配置Struts项目提示Cannot resolve symbol 'xx.jsp',以及没有找到Namespace为/的指定Action的解决
  6. html 隐藏input
  7. 机器人电焊电流电压怎么调_西安焊接机器人工作站的主要设备有哪些?西安瑞斯曼...
  8. Oracle 中给表添加主键、外键
  9. 87.http upstream模块提供的变量
  10. GJB150.18A-2009机械冲击试验测试要求
  11. android与rn混合开发,RN 混合开发项目调用安卓原生解决方案
  12. stm32固件库手册使用方法
  13. 开氏温度与摄氏度换算_温度是怎么来的,有没有物质没有温度?
  14. [转]GNSS NMEA-0183协议解析
  15. 学会ipad当作电脑扩展屏方法
  16. 什么是pptp,什么是vps?两者有何区别?
  17. Xshell6官网个人免费版下载
  18. Huawei 華為 MT7-TL10开机卡 logo卡开机画面 使用UFED完美导出客户数据资料
  19. 十进制数转换为二进制,八进制,十六进制数的算法(欢迎拍砖)
  20. windows下编译obs遇到的错误及解决

热门文章

  1. Observable.OnSubscribe 的理解
  2. 算法--------数组------反转字符串中的元音字母
  3. 【剑指offer-Java版】16反转链表
  4. java 打破双亲委派,为什么说java spi破坏双亲委派模型?
  5. python requests库用法_python之requests库的使用
  6. eclipse的tomcat如何运行自动弹网页_如何在 3 天内零成本完成 AI 小程序开发
  7. 【Android UI】theme style
  8. 利用 CoreGraphics 绘制折线图
  9. 网站性能优化之yahoo军规
  10. JavaScript的前世今生