博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Swift创建空数组
阅读量:4560 次
发布时间:2019-06-08

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

 

var yourArray = [String]()
yourArray.append("String Value")

Or

let someString = "You can also pass a string variable, like this!" yourArray.append(someString)

通过插入添加

 

一旦有了一些值,就可以插入新的值而不是追加。例如,如果要在数组的开头插入新对象(而不是将其追加到末尾):

yourArray.insert("Hey, I'm first!", atIndex: 0)
let lineCutter = "I'm going to be first soon." let positionToInsertAt = 0 yourArray.insert(lineCutter, atIndex: positionToInsertAt)
var yourOtherArray = ["MonkeysRule", "RemoveMe", "SwiftRules"] yourOtherArray.removeAtIndex(1)

当您知道数组中的值在哪里时(也就是说,当您知道它的索引值时),上面的方法非常有效。当索引值从0开始时,第二个条目将位于索引1。

 

在不知道索引的情况下删除值

 

但如果你不呢?如果你的数组有数百个值,你只知道你想删除一个等于“removeme”的值呢?

if let indexValue = yourOtherArray.indexOf("RemoveMe") { yourOtherArray.removeAtIndex(indexValue) }

转载于:https://www.cnblogs.com/gamecenter/p/11323512.html

你可能感兴趣的文章
搬运工程 启动!
查看>>
局部加权回归(LWR) Matlab模板
查看>>
Connect to the DSP on C6A8168/DM8168/DM8148 using CCS
查看>>
hibernate在使用getCurrentSession时提示no session found for current thread
查看>>
【Luogu1471】方差(线段树)
查看>>
DEV中svg图标的使用
查看>>
Codefroces Gym101572 I.Import Spaghetti-有向图跑最小环输出路径(Floyd)
查看>>
有关位运算的操作+二进制状态压缩
查看>>
Eclipse插件 -- 阿里巴巴扫描编码规插件
查看>>
(1.1)学习笔记之mysql体系结构(内存、进程、线程)
查看>>
markdown测试
查看>>
Java-Maven-Runoob:Maven 依赖管理
查看>>
杂项-Log:log4net
查看>>
杂项-Java:EL表达式
查看>>
tarroni music
查看>>
unity 使用RotateAround的使用注意
查看>>
[SDOI2009]HH的项链
查看>>
CodeFirst模式,容易引发数据迁移问题(不建议使用)
查看>>
jquery的colorbox关闭并传递数据到父窗
查看>>
使用Nginx、Keepalived构建文艺负载均衡
查看>>