博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
在Swift中向数组添加元素
阅读量:3579 次
发布时间:2019-05-20

本文共 2714 字,大约阅读时间需要 9 分钟。

本文翻译自:

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楼

参考:


#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 " ", starting on page 110. 更多信息可以从第110页开始的“ ”的“集合类型”一章中找到。


#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。


#4楼

To add to the end, use the += operator: 要添加到末尾,请使用+=运算符:

myArray += ["Craig"]myArray += ["Jony", "Eddy"]

That operator is generally equivalent to the method. 该运算符通常等效于方法。 (And in really old Swift versions, could append single elements, not just other collections of the same element type.) (在真正的旧Swift版本中,可以附加单个元素,而不仅仅是附加相同元素类型的其他集合。)

There's also for inserting at any index. 还有可以在任何索引插入。

If, say, you'd like a convenience function for inserting at the beginning, you could add it to the Array class with an . 举例来说,如果您希望在开始时插入一个方便的功能,则可以使用将其添加到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 appendmyArray.append(str1)myArray.append(str2)

To add them to the front: 要将它们添加到前面:

//use 'insert' instead of appendmyArray.insert(str1, atIndex:0)myArray.insert(str2, atIndex:0)//Swift 3myArray.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开始,您不能再使用'+ ='

转载地址:http://ellgj.baihongyu.com/

你可能感兴趣的文章
小甲鱼Python第二十三讲、第二十四讲(递归-这帮小兔崽子、汉诺塔)
查看>>
小甲鱼Python第二十七讲(集合)
查看>>
可调谐半导体激光器的窄线宽测试及压缩
查看>>
matlab中 %d,%f,%c,%s
查看>>
常见的光纤接头汇总
查看>>
半导体激光器—问题整理(二)
查看>>
科研日记7.31
查看>>
zemax仿真二向色镜
查看>>
stm32单片机编程时extern的用法
查看>>
UART4和5的问题
查看>>
Spring框架中在并发访问时的线程安全性
查看>>
网站部署
查看>>
什么情况下会发生栈内存溢出。
查看>>
何为去中心化
查看>>
缓存一致性:写策略
查看>>
Cache一致性:MESI
查看>>
缓存一致性:写未命中
查看>>
为什么用中间位作为组索引
查看>>
缓存:局部性
查看>>
mysql原理:b+树索引
查看>>