在Swift中使用flatMap保持索引

koe*_*oen 4 flatmap swift3

这是这个问题的后续行动:

flatMap和`成员`的模糊引用错误

在那里我使用以下代码将Records 数组转换为s数组Person:

let records = // load file from bundle
let persons = records.flatMap(Person.init)
Run Code Online (Sandbox Code Playgroud)

由于此转换可能需要一些时间来处理大文件,因此我希望监控索引以提供进度指示器.

这种flatMap结构有可能吗?我想到的一种可能性是在init函数中发送通知,但我在想从内部计算记录也是可能的flatMap

Ale*_*ica 14

对!使用enumerated().

let records = // load file from bundle
let persons = records.enumerated().flatMap { index, record in
    print(index)
    return Person(record)
}
Run Code Online (Sandbox Code Playgroud)