当前位置: 首页 > news >正文

swift菜鸟教程11-12(数组与字典)

一个朴实无华的目录

  • 今日学习内容:
    • 1.Swift 数组
      • 1.1创建数组
      • 1.2访问数组
      • 1.3修改数组
        • 使用 append() 方法或者赋值运算符 += 在数组末尾添加元素
        • 通过索引修改数组元素的值:
      • 1.4遍历数组 使用for-in循环
        • 同时需要每个数据项的值和索引值
      • 1.5合并数组
      • 1.6count 属性
      • 1.7isEmpty 属性
    • 2.Swift 字典
      • 2.1定义 :**存储无序的相同类型数据的集合**
      • 2.2创建字典
      • 2.3访问字典
      • 2.4修改字典:使用 updateValue(forKey:) 返回Optional值。
      • 2.5移除 Key-Value 对:removeValueForKey()
      • 2.6遍历字典
      • 2.7字典转换为数组
      • 2.8使用只读的 count 属性来计算字典有多少个键值对:

今日学习内容:

1.Swift 数组

1.1创建数组

以下实例创建了一个类型为 Int ,数量为 3,初始值为 0 的空数组:

var someInts = [Int](repeating: 0, count: 3)

以下实例创建了含有三个元素的数组:

var someInts:[Int] = [10, 20, 30]

1.2访问数组

import Cocoa

var someInts = [Int](repeating: 10, count: 3)

var someVar = someInts[0]

print( "第一个元素的值 \(someVar!)" )
print( "第二个元素的值 \(someInts[1]!)" )
print( "第三个元素的值 \(someInts[2]!)" )

1.3修改数组

使用 append() 方法或者赋值运算符 += 在数组末尾添加元素
import Cocoa

var someInts = [Int]()

someInts.append(20)
someInts.append(30)
someInts += [40]

var someVar = someInts[0]

print( "第一个元素的值 \(someVar)" )
print( "第二个元素的值 \(someInts[1])" )
print( "第三个元素的值 \(someInts[2])" )

通过索引修改数组元素的值:
import Cocoa

var someInts = [Int]()

someInts.append(20)
someInts.append(30)
someInts += [40]

// 修改最后一个元素
someInts[2] = 50

var someVar = someInts[0]

print( "第一个元素的值 \(someVar)" )
print( "第二个元素的值 \(someInts[1])" )
print( "第三个元素的值 \(someInts[2])" )

1.4遍历数组 使用for-in循环

import Cocoa

var someStrs = [String]()

someStrs.append("Apple")
someStrs.append("Amazon")
someStrs.append("Runoob")
someStrs += ["Google"]

for item in someStrs {
   print(item)
}

以上程序执行输出结果为:

Apple
Amazon
Runoob
Google

同时需要每个数据项的值和索引值
import Cocoa

var someStrs = [String]()

someStrs.append("Apple")
someStrs.append("Amazon")
someStrs.append("Runoob")
someStrs += ["Google"]

for (index, item) in someStrs.enumerated() {
    print("在 index = \(index) 位置上的值为 \(item)")
}

以上程序执行输出结果为:

在 index = 0 位置上的值为 Apple
在 index = 1 位置上的值为 Amazon
在 index = 2 位置上的值为 Runoob
在 index = 3 位置上的值为 Google

1.5合并数组

import Cocoa

var intsA = [Int](repeating: 2, count:2)
var intsB = [Int](repeating: 1, count:3)

var intsC = intsA + intsB

for item in intsC {
    print(item)
}

以上程序执行输出结果为:

2
2
1
1
1

1.6count 属性

import Cocoa

var intsA = [Int](count:2, repeatedValue: 2)
var intsB = [Int](count:3, repeatedValue: 1)

var intsC = intsA + intsB

print("intsA 元素个数为 \(intsA.count)")
print("intsB 元素个数为 \(intsB.count)")
print("intsC 元素个数为 \(intsC.count)")

以上程序执行输出结果为:

intsA 元素个数为 2
intsB 元素个数为 3
intsC 元素个数为 5

1.7isEmpty 属性

import Cocoa

var intsA = [Int](count:2, repeatedValue: 2)
var intsB = [Int](count:3, repeatedValue: 1)
var intsC = [Int]()

print("intsA.isEmpty = \(intsA.isEmpty)")
print("intsB.isEmpty = \(intsB.isEmpty)")
print("intsC.isEmpty = \(intsC.isEmpty)")

以上程序执行输出结果为:
intsA.isEmpty = false
intsB.isEmpty = false
intsC.isEmpty = true

2.Swift 字典

2.1定义 :存储无序的相同类型数据的集合

Swift 字典每个值(value)都关联唯一的键(key),键作为字典中的这个值数据的标识符。

2.2创建字典

var someDict = [Int: String]()
var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"]

2.3访问字典

import Cocoa

var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"]

var someVar = someDict[1]

print( "key = 1 的值为 \(someVar)" )
print( "key = 2 的值为 \(someDict[2])" )
print( "key = 3 的值为 \(someDict[3])" )

2.4修改字典:使用 updateValue(forKey:) 返回Optional值。

如果 key 不存在,则添加值,如果存在则修改 key 对应的值。

import Cocoa

var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"]

var oldVal = someDict[1]
someDict[1] = "One 新的值"
var someVar = someDict[1]

print( "key = 1 旧的值 \(oldVal)" )
print( "key = 1 的值为 \(someVar)" )
print( "key = 2 的值为 \(someDict[2])" )
print( "key = 3 的值为 \(someDict[3])" )

以上程序执行输出结果为:

key = 1 旧的值 Optional("One")
key = 1 的值为 Optional("One 新的值")
key = 2 的值为 Optional("Two")
key = 3 的值为 Optional("Three")

2.5移除 Key-Value 对:removeValueForKey()

如果 key 存在该方法返回移除的值,如果不存在返回 nil 。

import Cocoa

var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"]

var removedValue = someDict.removeValue(forKey: 2)

print( "key = 1 的值为 \(someDict[1])" )
print( "key = 2 的值为 \(someDict[2])" )
print( "key = 3 的值为 \(someDict[3])" )

也可以通过指定键的值为 nil 来移除 key-value(键-值)对

someDict[2] = nil

以上程序执行输出结果为:

key = 1 的值为 Optional("One")
key = 2 的值为 nil
key = 3 的值为 Optional("Three")

2.6遍历字典

import Cocoa

var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"]

for (key, value) in someDict {
   print("字典 key \(key) -  字典 value \(value)")
}

2.7字典转换为数组

import Cocoa

var someDict:[Int:String] = [1:"One", 2:"Two", 3:"Three"]

let dictKeys = [Int](someDict.keys)
let dictValues = [String](someDict.values)

print("输出字典的键(key)")

for (key) in dictKeys {
    print("\(key)")
}

print("输出字典的值(value)")

for (value) in dictValues {
    print("\(value)")
}

2.8使用只读的 count 属性来计算字典有多少个键值对:

import Cocoa

var someDict1:[Int:String] = [1:"One", 2:"Two", 3:"Three"]
var someDict2:[Int:String] = [4:"Four", 5:"Five"]

print("someDict1 含有 \(someDict1.count) 个键值对")
print("someDict2 含有 \(someDict2.count) 个键值对")

相关文章:

  • 使用django实现windows任务调度管理
  • 怎么样在Windows系统上安装 的 WPS JS 插件
  • AUTO-DL 910B + mindspeed-llm 4层DeepSeek V3微调
  • MQTT的构成、使用场景、工作原理介绍
  • EAL4+ vs EAL7:高安全场景下的等级选择策略
  • 面向对象高级(1)
  • 获取git分支间差异文件列表
  • QEMU学习之路(6)— RISC-V 启动Linux
  • 技术分享|iTOP-RK3588开发板Ubuntu20系统旋转屏幕方案
  • 蓝桥杯 15g
  • Matlab 电机激励模型和仿真
  • Linux上位机开发实践(mcu模块的补充应用)
  • 【常用功能】下载文件和复制到剪切板
  • Flink的 RecordWriter 数据通道 详解
  • vue2 el-element中el-select选中值,数据已经改变但选择框中不显示值,需要其他输入框输入值才显示这个选择框才会显示刚才选中的值
  • 【2】安装Nodejs-Nodejs开发入门
  • 直播电商革命:东南亚市场的“人货场”重构方程式
  • GNSS静态数据处理
  • 如何将网页保存为pdf
  • 【后端开发】Spring MVC-计算器、用户登录、留言板
  • c语言新手入门代码/优化好搜移动端关键词快速排名
  • 淘宝网站c 设计怎么做/搜索历史记录
  • 今天的新闻内容50字/北京关键词优化服务
  • 做博彩的网站赚钱吗/适合seo的建站系统
  • 网站正在建设中 色/上海网站排名推广
  • 工程机械网站设计/电商运营工资一般多少钱一个月