swift基础学习(03)[数组、集合、字典]
2016-05-08 09:46
281 查看
//数组
//空数组
var arr = [Int]()
print(arr)
//带有默认值s数组
var shoppingList: [String] = ["Eggs",
"Milk"]
var number :[Int] = [1,2,3]
print(shoppingList,number)
//数组追加创建新的数组
var number2 = [5,6,7]
var add = number2 +
number
print(add[0])
//快速遍历数组
for index in
add {
print(index)
}
//判断数组是否为空
if add.isEmpty {
print("数组为空")
}else{
print("数组不为空")
}
//为数组追加元素
add.append(9)
print(add)
//取出某个索引的值index
print(add[2])
//插入某个数
add.insert(11, atIndex:
0)
print(add)
//删除某个元素
add.removeAtIndex(0)
print(add)
//采用元组遍历获得
对应的索引和值
for (index,value) in
add.enumerate() {
print("index is
\(index),value is
\(value)")
}
//集合集合(Set)用来存储相同类型并且没有确定顺序的值。当集合元素顺序不重要时或者希望确保每个元素只出现一次
时可以使用集合而不是数组
//创建一个空的集合
var emptySet = Set<Character>()
print(emptySet.count)
//增
emptySet.insert("d")
print(emptySet)
//创建一个集合,这是一个string集合,其他类似
var combine: Set<String> = ["1","2"]
print(combine)
//遍历集合
for index in
combine {
print(index)
}
//其他类似数组
//字典
//key-value
//创建一个空字典
var emptyDic = [Int:String]()
print(emptyDic)
//直接
var contentDic = ["1":"nihao"]
print(contentDic)
//空数组
var arr = [Int]()
print(arr)
//带有默认值s数组
var shoppingList: [String] = ["Eggs",
"Milk"]
var number :[Int] = [1,2,3]
print(shoppingList,number)
//数组追加创建新的数组
var number2 = [5,6,7]
var add = number2 +
number
print(add[0])
//快速遍历数组
for index in
add {
print(index)
}
//判断数组是否为空
if add.isEmpty {
print("数组为空")
}else{
print("数组不为空")
}
//为数组追加元素
add.append(9)
print(add)
//取出某个索引的值index
print(add[2])
//插入某个数
add.insert(11, atIndex:
0)
print(add)
//删除某个元素
add.removeAtIndex(0)
print(add)
//采用元组遍历获得
对应的索引和值
for (index,value) in
add.enumerate() {
print("index is
\(index),value is
\(value)")
}
//集合集合(Set)用来存储相同类型并且没有确定顺序的值。当集合元素顺序不重要时或者希望确保每个元素只出现一次
时可以使用集合而不是数组
//创建一个空的集合
var emptySet = Set<Character>()
print(emptySet.count)
//增
emptySet.insert("d")
print(emptySet)
//创建一个集合,这是一个string集合,其他类似
var combine: Set<String> = ["1","2"]
print(combine)
//遍历集合
for index in
combine {
print(index)
}
//其他类似数组
//字典
//key-value
//创建一个空字典
var emptyDic = [Int:String]()
print(emptyDic)
//直接
var contentDic = ["1":"nihao"]
print(contentDic)
相关文章推荐
- swift基础学习(03)
- Swift解读专题二——基本类型
- 若干道Swift面试题
- 对照Java学习Swift--析构过程(Deinitialization)
- swift-struct
- Swift中把图片保存到相册
- 对照Java学习Swift--构造过程(Initialization)
- swift 新闻列表实现
- swift 新闻列表实现
- swift TabBar
- Swift-贝赛尔曲线实现画图板 && 截图保存到相册中
- Swift 有哪些优秀的第三方库?
- swift 泛型
- SWift开发:使用委托/Protocol 实现类之间的数据传递
- Swift中方法闭包参数不能省略括号的一种情况
- Swift中方法闭包参数不能省略括号的一种情况
- Swift中方法闭包参数不能省略括号的一种情况
- 【Swift学习】Swift编程之旅---析构方法(十九)
- 对照Java学习Swift--继承(Inheritance)
- 传智Swift入门视频第20期