Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/99.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在Switft中使用firstIndex查找所有结果_Ios_Arrays_Swift_Uikit - Fatal编程技术网

Ios 如何在Switft中使用firstIndex查找所有结果

Ios 如何在Switft中使用firstIndex查找所有结果,ios,arrays,swift,uikit,Ios,Arrays,Swift,Uikit,我试图将一个字符串拆分为一个字母数组,但将一些字母放在一起。(例如,我试着把他们分成发音组)。 因此,例如,所有的“sh”组合将是数组中的一个值,而不是两个值 使用firstIndex可以很容易地在一个我知道有“sh”的数组中找到一个“s”。但是,如何获得数组的第一个或最后一个索引之外的更多索引呢 Swift文件包括以下示例: let students = ["Kofi", "Abena", "Peter", "Kweku", "Akosua"] if let i = students.firs

我试图将一个字符串拆分为一个字母数组,但将一些字母放在一起。(例如,我试着把他们分成发音组)。 因此,例如,所有的“sh”组合将是数组中的一个值,而不是两个值

使用firstIndex可以很容易地在一个我知道有“sh”的数组中找到一个“s”。但是,如何获得数组的第一个或最后一个索引之外的更多索引呢

Swift文件包括以下示例:

let students = ["Kofi", "Abena", "Peter", "Kweku", "Akosua"]
if let i = students.firstIndex(where: { $0.hasPrefix("A") }) {
    print("\(students[i]) starts with 'A'!")
}
// Prints "Abena starts with 'A'!"
我如何获得Abena和Akosua(以及其他,如果有更多的话?)

下面是我的代码,它实现了我想要的一些功能(请原谅这个相当蹩脚的错误捕获)


您将获得Abena和Akosua(以及其他,如果有更多?)

然后你可以打电话

letterArray.allIndexes(of: "s") // [0, 4, 8, 10, 13, 18]
玩得开心

["Kofi", "Abena", "Peter", "Kweku", "Akosua"].forEach {
    if $0.hasPrefix("A") {
        print("\($0) starts with 'A'!")
    }
}

您可以筛选集合索引:

let students = ["Kofi", "Abena", "Peter", "Kweku", "Akosua"]
let indices = students.indices.filter({students[$0].hasPrefix("A")})
print(indices)  // "[1, 4]\n"

您还可以创建自己的索引方法,该方法采用谓词:

extension Collection {
    func indices(where predicate: @escaping (Element) throws -> Bool) rethrows -> [Index] {
        try indices.filter { try predicate(self[$0]) }
    }
}

用法:

let indices = students.indices { $0.hasPrefix("A") }
print(indices)  // "[1, 4]\n"

索引(of:)
,其中集合元素可
均衡

extension Collection where Element: Equatable {
    func indices(of element: Element) -> [Index] {
        indices.filter { self[$0] == element }
    }
}

用法:

let message = "she sells seashells"
let indices = message.indices(of: "s")
print(indices)
注意:如果您需要在字符串中查找子字符串的所有范围,您可以检查此项。

如果您确实想使用
firstIndex
方法,这里有一个递归(!)实现,只是为了好玩:D

extension Collection where Element: Equatable {

    /// Returns the indices of an element from the specified index to the end of the collection.
    func indices(of element: Element, fromIndex: Index? = nil) -> [Index] {
        let subsequence = suffix(from: fromIndex ?? startIndex)
        if let elementIndex = subsequence.firstIndex(of: element) {
            return [elementIndex] + indices(of: element, fromIndex: index(elementIndex, offsetBy: 1))
        }
        return []
    }

}
递归 给定集合中
n
元素
的实例,函数将被调用
n+1次(包括第一次调用)

复杂性 从复杂性来看,
后缀(from:)
是O(1),
firstIndex(of:)
是O(n)。假设
firstIndex
在遇到第一个匹配时终止,任何递归都会在我们离开的地方继续。因此,
索引(of:fromIndex:)
是O(n),就像使用
过滤器一样好。遗憾的是,这个函数不是尾部递归的…尽管我们可以通过保持一个运行总数来改变它

演出 [也许我下次再做这个。]

免责声明
递归很有趣,但你可能应该使用Leo Dabus的解决方案。

看起来像的副本,你可能想看看。打印(消息,字母数组)
的预期结果是什么?
让locate1=letterArray.firstIndex(of:“s”)//他不想首先
…他想得到所有放在s处的索引…他的计算基于他想得到的索引…再次…他想得到所有放在s处的索引…你也可以使用带有PartialRangeFrom
func index(of element:element,from index:index?=nil)->[index]{guard let index=self的下标[(index??startIndex)…].firstIndex(of:element)else{return[]}return[index]+索引(of:element,from:self.index(after:index))}
:)顺便说一句,创建单个项的集合时,如果需要有效地将单个值表示为集合,则可以使用CollectionFone实例。例如,可以使用带有连接运算符(+):
函数索引的CollectionFone实例将单个元素添加到数组中(of-element:element,from-index:index?=nil)->[index]{guard-let-index=self[(index??startIndex)…].firstIndex(of:element)或者{return[]}return-CollectionOfOne(index)+index(of:element,from:self.index(after:index))
我知道有一个递归解决方案!我很高兴有另一个:)但是thanks@TimSteen请注意,并非所有集合都从零开始。如果需要元素的索引(偏移量并不总是与索引相同),则应避免使用枚举偏移量(接受答案)方法
let message = "she sells seashells"
let indices = message.indices(of: "s")
print(indices)
extension Collection where Element: Equatable {

    /// Returns the indices of an element from the specified index to the end of the collection.
    func indices(of element: Element, fromIndex: Index? = nil) -> [Index] {
        let subsequence = suffix(from: fromIndex ?? startIndex)
        if let elementIndex = subsequence.firstIndex(of: element) {
            return [elementIndex] + indices(of: element, fromIndex: index(elementIndex, offsetBy: 1))
        }
        return []
    }

}