Swift收集扩展:选择其他每一项

Swift收集扩展:选择其他每一项,swift,dictionary,collections,Swift,Dictionary,Collections,我在Swift中为Collection编写了以下扩展,它从一个起始索引返回其他元素的新集合 extension Collection { func everyOtherElement(from theIndex: Int = 0) -> [Element] { if theIndex >= self.count { return self as! [Element] } let start = self.index(startIndex,

我在Swift中为Collection编写了以下扩展,它从一个起始索引返回其他元素的新集合

extension Collection {
    func everyOtherElement(from theIndex: Int = 0) -> [Element]  {
        if theIndex >= self.count { return self as! [Element] }

        let start = self.index(startIndex, offsetBy: theIndex)
        let end = self.endIndex
        var everyOther = [Element]()
        var iter = start

        while iter != end {
            everyOther.append(self[iter])
            let next = index(after: iter)
            if next == end { break }
            iter = index(after: next)
        }       
                 return everyOther
        }
}
可能有一些方法可以改进代码,但我的问题是当集合是一个字典时。扩展可以工作,但返回元组数组[(key:key,value:value)] 我想让分机返回一本字典

我尝试过Dictionary(uniqueKeysWithValuesMethod),该方法在应用于EveryoTheElement方法的返回时效果很好,但我似乎找不到一种直接实现它的方法

var myDictionary =  ["EN" : "Cheers", "SV" : "Skåll", "ES" : "Salud" ].everyOtherElement()
// returns [(key: "EN", value: "Cheers"),(key: "ES", value: "Salud")]

Dictionary(uniqueKeysWithValues: myDictionary.map { ($0.key, $0.value)})
// returns ["EN" : "Cheers", "ES" : "Salud" ]

谢谢你的帮助

正如其他人提到的,字典是一个无序的集合。如果您想返回一个字典,您可以简单地扩展字典并过滤其他元素。请注意,不能保证字典将保持输入键值对的顺序。也就是说,你可以通过以下方式实现你想要的:

extension Dictionary {
    var everyOtherElements: Dictionary {
        var bool = true
        return filter { _ in
            defer { bool = !bool }
            return bool
        }
    }
}

关于你的评论

我从你的回复中了解到,我不能用它来扩展所有的收藏 相同的代码,我需要按类型进行分解

您不需要扩展每一个元素类型,例如,您可以扩展
RangeReplacableCollection
协议并返回
Self
,这也将扩展
String
s:

extension RangeReplaceableCollection  {
    mutating func removeEvenIndexElements() {
        var bool = true
        removeAll { _ in
            defer { bool = !bool }
            return bool
        }
    }
    mutating func removeOddIndexElements() {
        var bool = false
        removeAll { _ in
            defer { bool = !bool }
            return bool
        }
    }
    func evenIndexElements() -> Self {
        var bool = true
        return filter { _ in
            defer { bool = !bool }
            return bool
        }
    }
    func oddIndexElements() -> Self {
        var bool = false
        return filter { _ in
            defer { bool = !bool }
            return bool
        }
    }
}


你的函数做不同的事情。您可以快进到给定的索引,然后删除其他每一项。这两个部分在字典的上下文中都没有意义,因为字典的顺序没有定义。
self-as![Element]
不能强制将所有集合强制转换为元素数组。一个字符串,它不等于一个
数组
您正在显式返回一个数组谢谢大家花时间回复!!!我确实返回了一个数组
[Element]
,因为我没有找到任何其他编译方法。因此,我的原始问题是:换句话说,如果可能的话,我如何更改代码,使其也适用于字典!谢谢你,利奥!我从你的回复中了解到,我不能用相同的代码扩展所有集合,我需要按类型(?)进行细分。
extension RangeReplaceableCollection  {
    mutating func removeEvenIndexElements() {
        var bool = true
        removeAll { _ in
            defer { bool = !bool }
            return bool
        }
    }
    mutating func removeOddIndexElements() {
        var bool = false
        removeAll { _ in
            defer { bool = !bool }
            return bool
        }
    }
    func evenIndexElements() -> Self {
        var bool = true
        return filter { _ in
            defer { bool = !bool }
            return bool
        }
    }
    func oddIndexElements() -> Self {
        var bool = false
        return filter { _ in
            defer { bool = !bool }
            return bool
        }
    }
}
var alphabet = "abcdefghijklmnopqrstuvwxyz"
alphabet.removeEvenIndexElements()
alphabet   // "bdfhjlnprtvxz"

var arr = [1,2,3,4,5,6,7,8,9,0]
arr.removeOddIndexElements()
arr        //  [1, 3, 5, 7, 9]