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

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

数组

  • Arrays

1、一个数组只能存储特定类型的数据;

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

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]
  • 数组基本操作
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、一个字典只能存储固定一种键和值的数据类型搭配

//隐式声明
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 = [:] //清空字典
  • 字典基本操作
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

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
}


相关文章
|
6月前
|
存储 Swift
在Swift编程语言中,字典(Dictionary)
在Swift编程语言中,字典(Dictionary)
76 3
|
6月前
|
存储 安全 Swift
在Swift中,数组(Array)
在Swift中,数组(Array)
55 3
|
Swift
22 Swift中的字典
Swift中的字典
61 0
|
Swift 索引
18 在Swift中创建一个数组
在Swift中创建一个数组
340 0
|
8天前
|
存储 Swift iOS开发
Swift 数组
10月更文挑战第27天
17 3
|
8天前
|
存储 Swift iOS开发
Swift 字典
10月更文挑战第27天
15 2
|
6月前
|
存储 安全 Swift
【Swift开发专栏】Swift中的集合类型:数组、字典与集合
【4月更文挑战第30天】本文探讨Swift的三种内置集合类型:数组、字典和集合。数组是有序元素集合,支持动态大小调整和类型安全;字典是无序键值对,适用于快速查找;集合是无序不重复元素集合,适合检查元素存在性和集合运算。理解这些特性和用法能提升Swift编程效率。
68 1
|
6月前
|
Swift 索引
在Swift中,要删除数组中特定索引位置的元素
在Swift中,要删除数组中特定索引位置的元素
101 3
|
6月前
|
存储 安全 Swift
在Swift编程语言中,数组(Array)
在Swift编程语言中,数组(Array)
72 3
|
6月前
|
存储 Swift
在Swift编程语言中,字典(Dictionary)
在Swift编程语言中,字典(Dictionary)
421 3