【Swift4】(3) 数组 | 字典 | 示例

2023-10-07 18:22:40 浏览数 (3)

数组

  • Arrays

1、一个数组只能存储特定类型的数据; 2、所存储的数据不一定是一个类的对象,可以是基础数据类型;

代码语言:javascript复制
var array = ["A","B","C"] //["A", "B", "C"]
var array2:[String] = ["A","B","C"] //["A", "B", "C"]
var array3:Array<String> = ["A","B","C"] //["A", "B", "C"]

array[0] = "AA" //"AA"
array //["AA", "B", "C"]

var array4 = [Int]() //[]

var array5 = Array<String>() //[]

var array6:[Int] = [] //[]

array = [String]() //清空数组
array

array6 = [Int](repeatElement(0, count: 10)) //[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

var array7 = [2,3,4]
var array8 = array6   array7 //两个数组合并 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 3, 4]
  • 数组基本操作
代码语言:javascript复制
var array = ["A","B","C"]

array.count //3
array.isEmpty //false

array.append("#") //["A", "B", "C", "#"]

array  = ["D","E"] //["A", "B", "C", "#", "D", "E"]

array.insert("hello", at: 0) //在数组位置上插入 ["hello", "A", "B", "C", "#", "D", "E"]

array.remove(at: 0) //"hello" 删除的元素

array.removeLast() //"E" 删除最后一个元素

array[0] = "AA" 
array //["AA", "B", "C", "#", "D"]

array[2...4] = ["CC","DD","EE"] //批量修改,数组赋给数组
array //["AA", "B", "CC", "DD", "EE"]

//遍历数组-1
for index in 0..<array.count
{
    print(array[index])
}
//遍历数组-2
for item in array
{
    print(item)
}

字典

  • 字典初始化

1、字典存储的数据是键和值的数据对 2、所存储的数据中,键和值可以是任意数据类型 3、一个字典只能存储固定一种键和值的数据类型搭配

代码语言:javascript复制
//隐式声明
var dict = [1:"a",2:"b",3:"c"]

var site = ["search":"google","web":"mooc"]

//显式声明

var dict2:Dictionary<Int,String> = [1:"a",2:"b",3:"c"]
var site2:Dictionary<String,String> = ["search":"google","web":"mooc"]

var dict3 = Dictionary<Int,String>() //声明一个空字典

dict = Dictionary<Int,String>() //清空字典

dict = [:] //清空字典
  • 字典基本操作
代码语言:javascript复制
var dict = [1:"a",2:"b",3:"c"]

var site = ["search":"google","web":"mooc"]
dict.count //返回数据对数
dict.isEmpty //是否空
//访问键值
dict[1] //"a"
site["search"] //"google"
dict[4] //nil
dict[0] = "0" //"0"
"seo:" site["search"]! //"seo:google"

site.updateValue("imaginecode", forKey: "web")
site //["web": "imaginecode", "search": "google"]

//遍历key,value
for (key,value) in site
{
    print("(key): (value)")
}
//遍历key
for key in site.keys{
    print(key)
}
//遍历val
for val in site.values{
    print(val)
}

Array(site.keys) //强制类型转换 ["web", "search"]
  • 数组与字典在App中的应用

1、列表

示例: UIKit

代码语言:javascript复制
import UIKit

let colors = [
    "blue":(red:93,green:138,blue:168),
    "sweet":(red:254,green:111,blue:94),
    "yellow":(red:255,green:239,blue:0),
    "orange":(red:255,green:140,blue:0),
    "violet":(red:143,green:0,blue:255),
    "fern":(red:113,green:188,blue:120),
    "gamboge":(red:228,green:155,blue:15),
    "cerise":(red:244,green:0,blue:161),
    "icterine":(red:252,green:247,blue:94),
    "jam":(red:165,green:11,blue:94)
]

//var backView = UIView(frame: CGRect(x:0, y:0, width: 320, height:colors.count * 50)
var backView = UIView(frame:CGRect(x: 0.0, y: 0.0, width: 320.0, height: CGFloat(colors.count * 50)) )
    
backView.backgroundColor = UIColor.black

var index = 0

for (colorName,rgbTuple)  in colors {
    var colorStripe = UILabel(frame: CGRect(x:0.0,y:CGFloat(index*50 5),width:320,height:40))
    colorStripe.backgroundColor = UIColor(red: CGFloat(rgbTuple.red/255), green: CGFloat(rgbTuple.green/255), blue: CGFloat(rgbTuple.blue/255), alpha: 1.0)
    
    var colorNameLabel = UILabel(frame: CGRect(x:0.0 ,y:0.0, width:320.0, height:40.0))
    colorNameLabel.font = UIFont(name: "Arial", size: 24.0)
    colorNameLabel.textAlignment = NSTextAlignment.right
    colorNameLabel.text = colorName
    colorStripe.addSubview(colorNameLabel)
    backView.addSubview(colorStripe)
    index =1
}

0 人点赞