swift 关于字典和数组 笔记

来源:互联网 发布:linux下语言包安装 编辑:程序博客网 时间:2024/05/29 03:20

/*
* 可变数组
*/
var array:[String] = []

//增加
array.append(“1”)

//删除
array.remove(at: 0)

/*
* 不可变数组 初始化之后 只能获取不能改变
*/
let noArray = [1,2,2]

/*
* 可变字典 内容为无序
*/
var dict = [1:32, 2:2, 3:3 ]

//增加
dict[56] = 122

//替换
dict[1] = 99

//删除
dict[1] = nil

/*
* 不可变字典 初始化之后 只能获取不能改变
*/
let nodict = [1,2,2]

//: Playground - noun: a place where people can play

import UIKit

/*
* 可变数组
*/
var array:[String] = [“34”]

//增加
array.append(“1”)

//删除
array.remove(at: 0)

//两个数组拼接
let arrayTwo = [“11”,”e”]

let addArray = array + arrayTwo

//数组遍历
for temp in addArray {

print(temp)

}

for (index, temp) in addArray.enumerated() {

print("索引\(index)")print("元素\(temp)")

}

var array = Int

array.forEach({

print($0)

})

array[0…2]

array.insert(0, at: array.startIndex)

array.removeLast()

array.removeFirst()

array.remove(at: 0)

array.removeAll()

addArray

/*
* 不可变数组 初始化之后 只能获取不能改变
*/
let noArray = [1,2,2]

/************************************/
/*
* 可变字典 内容为无序
*/
var dict = [1:32, 2:2, 3:3 ]

//增加
dict[56] = 122

//替换
dict[1] = 99

//删除
dict[1] = nil

/*
* 不可变字典 初始化之后 只能获取不能改变
*/
let nodict = [1,2,2]

let han = [1,1,1]

for model in han {

print("33")

}

var dictTwo = [6:2]

//两个字典合并
for model in dict {

let key = model.keylet value = model.valuedictTwo[key] = value

}

//字典遍历 无序
for temp in dictTwo {

print(temp)

}

for (index, temp) in dictTwo.enumerated() {

print("索引\(index)")print("元素\(temp)")

}

0 0
原创粉丝点击