swift学习笔记3 集合类型

注:英文部分来自官方文档

It is good practice to create immutable collections in all cases where the collection does not need to change. Doing so makes it easier for you to reason about your code and enables the Swift compiler to optimize the performance of the collections you create.

如果集合不需要改变的情况下,最好是创建不可变的集合。这样做可以更容易地推导你的代码,并且swift编译器也能更容易地优化你创建的集合。

数组

  • 把两个数组相加来创建新数组
    你可以把两个已存在的拥有兼容类型的数组相加来创建一个新的数组。
var anotherThreeDoubles = Array(repeating: 2.5, count: 3)
// anotherThreeDoubles 是 [Double]类型, 值是 [2.5, 2.5, 2.5]

var sixDoubles = threeDoubles + anotherThreeDoubles
// sixDoubles 也是 [Double]类型, 值是[0.0, 0.0, 0.0, 2.5, 2.5, 2.5]

You can add a new item to the end of an array by calling the array’s append(_:) method:

可以通过调用append(_:)方法把一个新的item添加到数组末尾

var shoppingList = ["Eggs", "Milk"]
shoppingList.append("Flour")
// shoppingList 现在包含 3 items, 

Alternatively, append an array of one or more compatible items with the addition assignment operator (+=):

另外一种方法是使用操作符+=来添加一个拥有兼容项的数组。

shoppingList += ["Baking Powder"]
// shoppingList 现在包含 4 items
shoppingList += ["Chocolate Spread", "Cheese", "Butter"]
// shoppingList 现在包含 7 items

You can use subscript syntax to change an existing value at a given index:

可以使用下标语法来修改一个在给定索引的值。

shoppingList[0] = "Six eggs"
// 数组的第一个值现在是 "Six eggs" 而不是"Eggs"

When you use subscript syntax, the index you specify needs to be valid. For example, writing shoppingList[shoppingList.count] = “Salt” to try to append an item to the end of the array results in a runtime error.

当你使用下标语法时,你指定的下标必须是有效的。举个例子,
shoppingList[shoppingList.count] = "Salt"
上面的代码中,下标越界了,会报一个运行时错误。

You can also use subscript syntax to change a range of values at once, even if the replacement set of values has a different length than the range you are replacing. The following example replaces “Chocolate Spread”, “Cheese”, and “Butter” with “Bananas” and “Apples”:

你还可以使用下标语法一次性地修改一个范围内的值,即使用来替换的值的长度与被替换的范围的长度不一致。下面的例子用”Bananas” 和 “Apples”替换 “Chocolate Spread”, “Cheese”, 和 “Butter”

shoppingList[4...6] = ["Bananas", "Apples"]
// shoppingList 现在包含 6 items

If you need the integer index of each item as well as its value, use the enumerated() method to iterate over the array instead. For each item in the array, the enumerated() method returns a tuple composed of an integer and the item. The integers start at zero and count up by one for each item; if you enumerate over a whole array, these integers match the items’ indices. You can decompose the tuple into temporary constants or variables as part of the iteration:

如果你需要每个元素的值与索引的值,可以使用enumerated()去遍历数组。对于数组中的每一项,enumerated()方法都会返回一个包含一个整数和该元素的tuple。整数值从0开始,每次加1。如果你遍历整个数组,这些整数会和元素的索引向吻合。你可以把tuple解析为遍历的一部分的临时常量或变量

for (index, value) in shoppingList.enumerated() {
    print("Item \(index + 1): \(value)")
}
// Item 1: Six eggs
// Item 2: Milk
// Item 3: Flour
// Item 4: Baking Powder
// Item 5: Bananas

Set类型

  • set的基础操作
    下图显示了两个set a 和 b的各种操作的结果

Paste_Image.png

Use the intersection(_:) method to create a new set with only the values common to both sets.
Use the symmetricDifference(_:) method to create a new set with values in either set, but not both.
Use the union(_:) method to create a new set with all of the values in both sets.
Use the subtracting(_:) method to create a new set with values not in the specified set.

intersection(_:)方法用于取两个set都拥有的值。
symmetricDifference(_:) 方法用于取两个set中不共同拥有的值。
union(_:) 用于取两个set中的所有的值。
subtracting(_:)方法用于取不在某个特定set中的值。

let oddDigits: Set = [1, 3, 5, 7, 9]
let evenDigits: Set = [0, 2, 4, 6, 8]
let singleDigitPrimeNumbers: Set = [2, 3, 5, 7]

oddDigits.union(evenDigits).sorted()
// [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
oddDigits.intersection(evenDigits).sorted()
// []
oddDigits.subtracting(singleDigitPrimeNumbers).sorted()
// [1, 9]
oddDigits.symmetricDifference(singleDigitPrimeNumbers).sorted()
// [1, 2, 9]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值