如何从[(String?,[Contact])]()获取联系人数组?
我var contacts = [(String?, [Contact])]()需要提取所有联系人数组以将其var filteredContacts = [Contact]()放入搜索逻辑中。更新 Account 和 Accounts 对象 //Account
struct Account: Codable {
var id, displayName:String
var contacts: [Contact]?
...
}
//账户
struct Accounts: Codable {
let accounts: [Account]
...
}
switch result {
case .success(let result):
self.accounts = result.accounts
self.contacts = Array(result.accounts
.compactMap { account in account.contacts.map { (account.displayName , $0) } }.dropFirst())
self.filteredContacts = ??
回答
您可以简单地使用flatMap元组的第二个元素作为keyPath:
self.filteredContacts = contacts.flatMap(.1) // for older swift syntax you can use `flatMap { $1 }`