Swift Set update()

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)

其中,setSet 类的一个对象。


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"]
你觉得这篇文章有帮助吗?

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战