在Swift中向数组添加元素
本文翻译自:Add an element to an array in Swift
Suppose I have an array, for example: 假设我有一个数组,例如:
var myArray = ["Steve", "Bill", "Linus", "Bret"]
And later I want to push/append an element to the end of said array, to get: 然后我想将一个元素推/追加到所述数组的末尾,以获得:
["Steve", "Bill", "Linus", "Bret", "Tim"]
What method should I use? 我应该使用什么方法?
And what about the case where I want to add an element to the front of the array? 那我想在数组的前面添加一个元素呢? Is there a constant time unshift? 是否有固定的时间不变?
#1楼
参考:https://stackoom.com/question/1ciCr/在Swift中向数组添加元素
#2楼
As of Swift 3 / 4 , this is done as follows. 从Swift 3/4开始 ,操作如下。
To add a new element to the end of an Array. 向数组末尾添加新元素。
anArray.append("This String")
To append a different Array to the end of your Array. 将另一个数组附加到数组的末尾。
anArray += ["Moar", "Strings"]
anArray.append(contentsOf: ["Moar", "Strings"])
To insert a new element into your Array. 向您的数组中插入一个新元素。
anArray.insert("This String", at: 0)
To insert the contents of a different Array into your Array. 将另一个数组的内容插入到数组中。
anArray.insert(contentsOf: ["Moar", "Strings"], at: 0)
More information can be found in the "Collection Types" chapter of " The Swift Programming Language ", starting on page 110. 更多信息可以从第110页开始的“ Swift编程语言 ”的“集合类型”一章中找到。
#3楼
From page 143 of The Swift Programming Language: 从The Swift Programming Language的143页开始:
You can add a new item to the end of an array by calling the array's append method 您可以通过调用数组的append方法将新项目添加到数组的末尾
Alternatively, add a new item to the end of an array with the addition assignment operator (+=) 或者,使用加法赋值运算符(+ =)将新项添加到数组的末尾
Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks. 摘自:Apple Inc.“快速编程语言”。iBooks。 https://itun.es/us/jEUH0.l https://itun.es/us/jEUH0.l
#4楼
To add to the end, use the +=
operator: 要添加到末尾,请使用+=
运算符:
myArray += ["Craig"]
myArray += ["Jony", "Eddy"]
That operator is generally equivalent to the append(contentsOf:)
method. 该运算符通常等效于append(contentsOf:)
方法。 (And in really old Swift versions, could append single elements, not just other collections of the same element type.) (在真正的旧Swift版本中,可以附加单个元素,而不仅仅是附加相同元素类型的其他集合。)
There's also insert(_:at:)
for inserting at any index. 还有insert(_:at:)
可以在任何索引insert(_:at:)
插入。
If, say, you'd like a convenience function for inserting at the beginning, you could add it to the Array
class with an extension . 举例来说,如果您希望在开始时插入一个方便的功能,则可以使用extension将其添加到Array
类中。
#5楼
你可以用
Myarray.insert("Data #\(index)", atIndex: index)
#6楼
You can also pass in a variable and/or object if you wanted to. 如果需要,还可以传入变量和/或对象。
var str1:String = "John"
var str2:String = "Bob"var myArray = ["Steve", "Bill", "Linus", "Bret"]//add to the end of the array with append
myArray.append(str1)
myArray.append(str2)
To add them to the front: 要将它们添加到前面:
//use 'insert' instead of append
myArray.insert(str1, atIndex:0)
myArray.insert(str2, atIndex:0)//Swift 3
myArray.insert(str1, at: 0)
myArray.insert(str2, at: 0)
As others have already stated, you can no longer use '+=' as of xCode 6.1 正如其他人已经指出的那样,从xCode 6.1开始,您不能再使用'+ ='
在Swift中向数组添加元素相关推荐
- js中给数组添加元素的方法有哪些
js中给数组添加元素的方法有哪些 unshift:将参数添加到原数组开头,并返回数组的长度 pop:删除原数组最后一项,并返回删除元素的值:如果数组为空则返回undefined push:将参数添加到 ...
- swft c 语言 数组,如何在swift中实现数组的深拷贝
在Objective-C中如果想将一个数组赋值给另外一个数组,同时想让两个数组之间相互独立(即改变其中的一个数组,不影响另外的一个),有很多的办法,比如我们可以直接copy,用类方法创建新数组.这样得 ...
- JavaScript中给数组添加元素的方法
1 .push(字符/数字/数组) 将一个或多个元素添加到数组的尾部,并返回该数组的新长度 var arr = [1, 2, 3];arr.push('加油啊', 5, 6, [7, 21, 35]) ...
- java 数组中插入元素_Java数组添加元素
java 数组中插入元素 How to add elements to an array in java We know that java array size is fixed, so we ca ...
- 记OC迁移至swift中笔记20tips
写久了OC后来写swift,总感觉写着是swift的皮毛,但是实际上是OC的核心,这里整理了OC迁移至swift中的一些小细节. 1 在当前类中,实例方法调用属性以及方法都可以将self省略掉,而且是 ...
- javascript字典中添加数组_Javascript 数组与字典
Javascript 的数组Array,既是一个数组,也是一个字典(Dictionary). 先举例看看数组的用法. var a = new Array(); a[0] = "Acer&qu ...
- 1. Swift 中的 let 和 var
Swift 中的变量声明相对于 OC 中简化了很多.可变与不可变取决于声明的方式,不在取决于类型了,这样简化了很多不必要的类型.比如 OC 中可变的为 NSMutableArray,不可变的是 NSA ...
- 如何在Swift中串联或合并数组?
本文翻译自:How do I concatenate or merge arrays in Swift? If there are two arrays created in swift like t ...
- Swift中的集合类之数组
好丢人,最近写了一个摸不着东南西北的bug 说白了还是欠缺火候,下面总结一下Swift中的集合类... 空数组: [类型]() let array1 = [String]() Array<类型& ...
最新文章
- Python3协程(coroutine)理解
- P1021 邮票面值设计
- Centos7.6环境Docker安装Oracle19c企业版
- 【Elasticsearch】Elasticsearch 缓存策略 QueryCacheingPolicy
- 无人驾驶(再谈基于camera的高精度地图)
- SAP License:SAP概念辨识
- html 遮罩层在底部,底部弹出遮罩层.html
- Peter Norvig:学习在于挑战和重复
- 【08】英语词汇速记大全1词根词缀记忆法
- 【jQuery】基础知识梳理笔记
- 艾司博讯:拼多多价格竞争力不足怎么办
- SpringMVC的核心架构示意图<搬代码>
- android连接ios热点超时,Android19连接iOS13个人热点失败
- python两个表格数据核对,并把正确,错误,缺少的数据用颜色标记出来
- 北航计算机科学与技术应用工程专业,北京航空航天大学王牌专业?软件工程全国第一!...
- 浙大计应及国家公派留学
- Java版本企业电子招标采购系统源码:营造全面规范安全的电子招投标环境,促进招投标市场健康可持续发展
- 计世独家:新加坡国家网格雏形已现
- vscode缩进和Eslint缩进问题解决
- KRecyclerView