前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Swift4】(3) 数组 | 字典 | 示例

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

作者头像
前端修罗场
发布2023-10-07 18:22:40
1400
发布2023-10-07 18:22:40
举报
文章被收录于专栏:Web 技术

数组

  • 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
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 字典
相关产品与服务
对象存储
对象存储(Cloud Object Storage,COS)是由腾讯云推出的无目录层次结构、无数据格式限制,可容纳海量数据且支持 HTTP/HTTPS 协议访问的分布式存储服务。腾讯云 COS 的存储桶空间无容量上限,无需分区管理,适用于 CDN 数据分发、数据万象处理或大数据计算与分析的数据湖等多种场景。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档