swift 集合类型(数组、集合、字典)

2023-11-22 09:08:53 浏览数 (2)

数组

  • 初始化
代码语言:javascript复制
//创建一个空数组
let array = [Int]()
//创建具有默认值的数组
let arr1 = Array(repeating: 2, count: 3)  //[2, 2, 2]
//用数组字面量构造数组
let arr2: [String] = ["a","b"]
var arr3 = ["a","b"]
  • 元素个数
代码语言:javascript复制
// 获取数组元素个数
arr2.count
//判断数组count是否等于0
if arr2.isEmpty {
    print("The arr2 is empty.")
}
  • 添加新元素
代码语言:javascript复制
//尾部添加
var arr = [Int]()
arr.append(5)
print(arr)  //[5]

//指定索引添加
var arr2: [String] = ["a","b","c","d","e","f","g"]
arr2.insert("C", at: 2)
print(arr2)
//["a", "b", "C", "c", "d", "e", "f", "g"]
  • 拼合
代码语言:javascript复制
var arr = [Int]()
let arr1 = Array(repeating: 2, count: 3)
arr  = arr1 // arr:[2, 2, 2]
  • 通过数组索引检索值
代码语言:javascript复制
let arr2: [String] = ["a","b"]
var value = arr2[0]
  • 元素修改
代码语言:javascript复制
var arr2: [String] = ["a","b"]
arr2[0] = "c"
print(arr2)  //["c", "b"]


var arr3: [String] = ["a","b","c","d","e","f","g"]
arr3[2...5] = ["x","y","z"]
print(arr3)  //["a", "b", "x", "y", "z", "g"]
  • 元素删除
代码语言:javascript复制
var arr: [String] = ["a","b","c","d","e","f","g"]
arr.remove(at: 2)
print(arr)  // ["a", "b", "d", "e", "f", "g"]

arr.removeLast()
print(arr)  // ["a", "b", "d", "e", "f"]

arr.removeAll()

数组遍历

代码语言:javascript复制
var arr: [String] = ["a","b","c"]
for item in arr {
    print(item)
}
for (index ,value) in arr.enumerated() {
    print("(index)---(value)")
}
//a
//b
//c
//0---a
//1---b
//2---c

集合

  • 初始化
代码语言:javascript复制
//创建一个空集合
var set = Set<String>()
//用数组字面量构造
var set :Set<String> = ["a","b","c"]
var set :Set = ["a","b","c"]
  • 元素个数
代码语言:javascript复制
// 获取数组元素个数
set.count
//判断数组count是否等于0
if set.isEmpty {
    print("The set is empty.")
}
  • 添加新元素
代码语言:javascript复制
var set :Set<String> = ["a","b","c"]
set.insert("d")
print(set)
//["b", "a", "d", "c", "e"]
  • 元素删除
代码语言:javascript复制
var set :Set<String> = ["a","b","c"]
set.remove("b")
print(set)  //["c", "a"]

set.removeAll()

集合操作

代码语言:javascript复制
let set :Set<String> = ["a","b","c"]
let set1 :Set<String> = ["c","v","e","a","f","d"]
//交集
print(set.intersection(set1))
//["c", "a"]

//并集
print(set.union(set1))
//["e", "v", "b", "c", "a", "d", "f"]

//去除交集
print(set.symmetricDifference(set1))
//["e", "f", "v", "b", "d"]

//一个值不在指定集中的新集
print(set.subtracting(set1))
//["b"]

//判断一个集合中的值是否也被包含在另外一个集合中
print(set1.isSubset(of: set))   //false

//判断一个集合中包含另一个集合中所有的值
print(set1.isSuperset(of: set)) //false

//判断一个集合是否是另外一个集合的子集合或者父集合并且两个集合并不相等
print(set1.isStrictSubset(of: set))   //false

//判断两个集合是否不含有相同的值(是否没有交集)
print(set1.isStrictSubset(of: set))   //false

集合遍历

代码语言:javascript复制
for value in set {
    print(value)
}
//a  c  b

//集合先排序在遍历
for genre in set.sorted() {
    print("(genre)")
}

字典

  • 初始化
代码语言:javascript复制
//构造语法
var dic = [Int: String]()
//创建具有默认值的字典
var dic : [String:String] = ["key":"value"]
var dic = ["key":"value"]

  • 元素个数
代码语言:javascript复制
// 获取数组元素个数
var dic : [String:String] = ["key":"value","key1":"value1"]
print(dic.count)

//判断数组count是否等于0
if dic.isEmpty {
    print("the dic is empty")
}

  • 添加元素
代码语言:javascript复制
var dic : [String:String] = ["key":"a","key1":"b"]
dic["key2"] = "c"
print(dic)  //["key1": "b", "key2": "c", "key": "a"]

  • 修改元素
代码语言:javascript复制
//使用下标语法来改变特定键对应的值
var dic : [String:String] = ["key":"a","key1":"b"]
dic["key1"] = "B"
print(dic)  //["key": "a", "key1": "B"]
代码语言:javascript复制
var dic : [String:String] = ["key":"a","key1":"b"]

//如果有对应的key,那么会直接更新对应的value,且返回旧值
let oldValue = dic.updateValue("C", forKey: "key1")
print(dic)  //["key1": "C", "key": "a"]
print(oldValue)    //Optional("b")

//如果没有对应的key,那么会直接添加元素,返回nil
let oldValue1 = dic.updateValue("D", forKey: "key2")
print(dic)  //["key1": "C", "key2": "D", "key": "a"]
print(oldValue1)    //nil

  • 删除
代码语言:javascript复制
var dic : [String:String] = ["key":"a","key1":"b"]
dic["key1"] = nil
print(dic)  //["key": "a"]
dic.removeValue(forKey: "key1")
print(dic)  //["key": "a"]

  • 字典遍历
代码语言:javascript复制
let dic : [String:String] = ["key":"a","key1":"b"]
for (key,value) in dic {
    print("(key)---(value)")
}    //key---a    key1---b

for key in dic.keys {
    print("(key)")
}    //key  key1

for value in dic.values {
    print("(value)")
}    //a  b

字符串、数组、和字典类型的赋值与复制行为

Swift 中,许多基本类型,诸如String,Array和Dictionary类型均以结构体的形式实现。这意味着被赋值给新的常量或变量,或者被传入函数或方法中时,它们的值会被拷贝。

Objective-C 中NSString,NSArray和NSDictionary类型均以类的形式实现,而并非结构体。它们在被赋值或者被传入函数或方法时,不会发生值拷贝,而是传递现有实例的引用。

0 人点赞