泛型 – 在Swift中实现Set.addSequence
发布时间:2020-12-14 05:43:09 所属栏目:百科 来源:网络整理
导读:我在Swift中实现了一个使用Dictionary键的Set.我想实现一个addAll(序列)方法,该方法在Set中的Elements上采用任何序列类型,但是我得到一个没有意义的错误.这是我的代码 struct SetElement: Hashable { var hash = [Element: Bool]() init(elements: [Element]
我在Swift中实现了一个使用Dictionary键的Set.我想实现一个addAll(序列)方法,该方法在Set中的Elements上采用任何序列类型,但是我得到一个没有意义的错误.这是我的代码
struct Set<Element: Hashable> { var hash = [Element: Bool]() init(elements: [Element] = []) { for element in elements { self.hash[element] = true } } var array: [Element] { return hash.keys.array } func contains(element: Element) -> Bool { return hash[element] ?? false } mutating func add(element: Element) { hash[element] = true } mutating func add(array: [Element]) { for element in array { hash[element] = true } } mutating func add<S : SequenceType where S.Generator.Element == Element>(sequence: S) { for element in sequence { // Error here: "Cannot convert the expression's type 'S' to type 'S' hash[element] = true } } mutating func remove(element: Element) { hash[element] = nil } } 我在XCode 6.1和6.0.1中收到此错误. 我想遵循Array的extend方法的语义,但是这种类型的签名甚至都不能为我编译. 我做错了什么,还是应该提交雷达? 编辑: public mutating func extend<S : SequenceType where S.Generator.Element == Element>(sequence: S) { // Note that this should just be for each in sequence; this is working around a compiler crasher. for each in [Element](sequence) { insert(each) } } 然而,这只是将序列转换为数组,这种类型完全违背了SequenceType的目的.
更新:这已经在Swift 1.2(Xcode 6.3 beta 3)中得到修复,问题的原始代码编译没有错误. (另外,定义
因为Swift 1.2具有自定义集类型,所以不再需要 内置的本机Set类型.) 老答案:对我来说这看起来像个错误,但也许有人可以解释它. 可能的解决方法: >将sequence参数转换为SequenceOf< Element>明确: mutating func add<S : SequenceType where S.Generator.Element == Element>(sequence: S) { for element in SequenceOf<Element>(sequence) { hash[element] = true } } >(与https://stackoverflow.com/a/27181111/1187415相同)更换for循环 mutating func add<S : SequenceType where S.Generator.Element == Element>(sequence: S) { var gen = sequence.generate() while let element : Element = gen.next() { hash[element] = true } } >(从“Creating a Set Type in Swift”开始)使用地图: mutating func add<S : SequenceType where S.Generator.Element == Element>(sequence: S) { map(sequence) { self.hash[$0] = true } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |