加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何在Swift中创建唯一对象列表的数组

发布时间:2020-12-14 06:09:09 所属栏目:百科 来源:网络整理
导读:我们如何在Swift语言中创建唯一的对象列表,如NSSet Objective-C中的NSMutableSet。 从Swift 1.2(Xcode 6.3测试版),Swift有一个本机集类型。 从发行说明: A new Set data structure is included which provides a generic collection of unique elements,w
我们如何在Swift语言中创建唯一的对象列表,如NSSet& Objective-C中的NSMutableSet。
从Swift 1.2(Xcode 6.3测试版),Swift有一个本机集类型。
从发行说明:

A new Set data structure is included which provides a generic
collection of unique elements,with full value semantics. It bridges
with NSSet,providing functionality analogous to Array and Dictionary.

这里有一些简单的用法示例:

// Create set from array literal:
var set = Set([1,2,3,1])

// Add single elements:
set.insert(4)
set.insert(3)

// Add multiple elements:
set.unionInPlace([ 4,5,6 ])
// Swift 3: set.formUnion([ 4,6 ])

// Remove single element:
set.remove(2)

// Remove multiple elements:
set.subtractInPlace([ 6,7 ])
// Swift 3: set.subtract([ 6,7 ])

print(set) // [5,1,4]

// Test membership:
if set.contains(5) {
    print("yes")
}

但有更多的方法可用。

更新:集合现在也记录在Swift文档的“Collection Types”章节中。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读