insert()
方法将新元素添加到集合的末尾。
示例
var city: Set = ["Boston", "Tokyo", "Kathmandu"]
// add "London" to the city set
city.insert("London")
print(city)
// Output: ["Kathmandu", "Tokyo", "Boston", "London"]
insert() 语法
集合 insert()
方法的语法是
set.insert(newElement)
这里,set 是 set
类的一个对象。
insert() 参数
insert()
方法接受一个参数
- newElement - 要添加到 set 的元素
insert() 返回值
insert()
方法不返回任何值。它只更新当前的 set。
示例:Swift 集合 insert()
var languages: Set = ["Swift", "C", "Java"]
// add "C++" to the languages set
languages.insert("C++")
print(languages)
var priceList: Set = [12, 21, 35]
// add 44 to the priceList set
priceList.insert(44)
print(priceList)
输出
["C", "Java", "Swift", "C++"] [44, 21, 12, 35]