在字典中过滤数组-Swift [英] Filtering an Array inside a Dictionary - Swift

查看:151
本文介绍了在字典中过滤数组-Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试搜索带索引的字典,以根据客户端的姓氏返回特定的客户端.以下是我正在使用的数据结构.每个客户端对象都有一个String的name属性.

I am trying to search through a indexed dictionary to return a specific client based on the client's last name. Below are the data structures I am using. Each client object has a name property which is a String.

var clients = Client.loadAllClients()     //Returns client array
var contacts = [String: [Client]]()      //Indexed clients in a dictionary 
var letters: [String] = [] 

var filteredClient = [Client]()
var shouldShowSearchResults = false
var searchController : UISearchController!   

当我建立索引时,通讯录将返回:

When I do my indexing, the contacts dictionary returns:

{A: [Client("Andrew")]}

字母数组返回:

[A]

我正在使用UISearchController来显示经过筛选的客户端数组.

I am using the UISearchController to display the filtered array of clients.

func updateSearchResults(for searchController: UISearchController) {
    // how to filter the dictionary 
    self.tableView.reloadData()
}

但是,我不知道如何过滤字典以返回正确的客户列表.我尝试使用

However, I have no idea how to filter the dictionary to return the correct list of clients. I have tried to use

contacts.filter(isIncluded: ((key: String, value: [Client])) throws -> Bool((key: String, value: [Client])) throws -> Bool)

但是我对实现感到非常困惑.我正在使用Xcode 8.0和Swift 3.0.

But I was very confused about the implementation. I am using Xcode 8.0 and Swift 3.0.

如果有人能指出我正确的方向,那将不胜感激.请让我知道是否需要澄清任何事情.先感谢您.完整的代码可以在我的 Github

If anyone could point me in the right direction, that would be much appreciated. Please let me know if I need to clarify anything. Thank you in advance. The full code can be found at my Github

推荐答案

主要问题是您正在使用字典作为数据源数组.

The main problem is that you are using a dictionary as data source array.

我的建议是使用自定义结构作为模型

My suggestion is to use a custom struct as model

struct Contact {
   let letter : String
   var clients : [Client]

   init(letter: String, clients : [Client] = [Client]()) {
      self.letter = letter
      self.clients = clients
   } 

   mutating func add(client : Client) {
        clients.append(client)
   }
}

然后创建您的数据源数组

Then create your data source array

var contacts = [Contact]()  

和字母数组作为计算属性

and the letter array as computed property

var letters : [String] = {
   return contacts.map{ $0.letter }
}

按字母排序数组很容易

contacts.sort{ $0.letter < $1.letter }


现在您可以通过这种方式进行搜索/过滤(text是要搜索的文本)


Now you can search / filter this way (text is the text to be searched for)

 filteredClient.removeAll()
 for contact in contacts {
     let filteredContent = contact.clients.filter {$0.name.range(of: text, options: [.anchored, .caseInsensitive, .diacriticInsensitive]) != nil }
     if !filteredContent.isEmpty {
         filteredClient.append(filteredContent)
     }
 }

如果将filteredClient也声明为[Contact],并使用过滤后的项目创建临时的Contact实例,甚至可以保留这些部分(字母).

You can even keep the sections (letters) if you declare filteredClient also as [Contact] and create temporary Contact instances with the filtered items.

当然,您需要更改所有表视图数据源/委托方法以符合Contact数组,但这是值得的.数组作为数据源比字典更有效.

Of course you need to change all table view data source / delegate methods to conform to the Contact array, but it's worth it. An array as data source is more efficient than a dictionary.

这篇关于在字典中过滤数组-Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆