update()
方法将给定的元素插入到集合中。
示例
// create a dictionary with two elements
var information: Set = [54, 34, 35]
// insert 76 to information
information.update(with: 76)
// print updated dictionary
print(information)
// Output: [34, 54, 76, 35]
update() 语法
update()
方法的语法是
set.update(with: newElement)
其中,set 是 Set
类的一个对象。
update() 参数
update()
方法接受一个参数
newElement
- 要插入到 set 中的新元素
update() 返回值
update()
方法不返回任何值。它仅更新 set。
示例:Swift Set update()
// create a set with three elements
var employees: Set = ["Sabby", "Nick", "Cathy"]
print("Before:", employees)
// insert "Katty" to employees
employees.update(with: "Katty")
// print updated set
print("Updated:", employees)
输出
Before: ["Cathy", "Sabby", "Nick"] Updated: ["Katty", "Cathy", "Sabby", "Nick"]