Dictionary 키를 사용하는 Set in Swift를 구현했습니다. 집합의 요소를 통해 모든 시퀀스 형식을 사용하는 addAll (시퀀스) 메서드를 구현하고 싶지만 이해가되지 않는 오류가 발생합니다. 여기 내 코드가Swift에서 Set.addSequence 구현
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의 확장 메소드의 의미를 따르기를 원했지만 그 유형 서명은 나를 위해 컴파일되지 않습니다.
나는 잘못된 것을하고 있습니까, 아니면 레이더 파일을해야합니까?
편집 : 그냥 https://github.com/robrix/Set/blob/master/Set/Set.swift을 발견,이 구현이 있습니다
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
의 목적을 패배 Array
로 sequence
변환합니다.
이 버그에 동의합니다. 누군가보고 했니? 고맙습니다. – drasto
@drasto : 잘 모르겠습니다. 그러나 어떤 경우이 문제의 영향을 받으면 자신의 버그 보고서를 제출하십시오. –
'map '을 사용하는 것이 가장 좋은 해결 방법 인 것 같습니다. 나는 거기에있는 주요한 문제는'map'이 실제로 부작용을 허용해서는 안된다는 것입니다. – bart