如何在Model中搜索数组属性

时间:2016-04-11 04:30:19

标签: ios swift uisearchcontroller

我必须制作类似联系人的应用。我有一个模型对象。请注意2个属性phoneNumbers& addresses是数组

class Contact: NSObject {

var name: String?
var companyName: String?
var phoneNumbers: [String]?
var addresses: [String]?

.. // Custom init methods etc

}

现在我必须填充这些并使用搜索视图控制器搜索它们。

我按照简单的方法,在TableViewController中实现了一个searchViewController。很像这里:https://www.raywenderlich.com/113772/uisearchcontroller-tutorial

func filterContentForSearchText(searchText: String, scope: String = "All") {
    filteredContacts.removeAll(keepCapacity: false)

    let searchPredicate = NSPredicate(format: "name CONTAINS[c] %@ OR companyName CONTAINS[c] %@", searchController.searchBar.text!, searchController.searchBar.text!, searchController.searchBar.text!)

    let array = (contacts as NSArray).filteredArrayUsingPredicate(searchPredicate)
    filteredContacts = array as! [Contact]

    tableView.reloadData()
}

我的问题是如何搜索电话号码和地址数组?上面的代码可以很好地搜索非数组属性,但是如何搜索数组呢?

1 个答案:

答案 0 :(得分:0)

以下是您在操场中粘贴的代码,用于测试在数组属性中的搜索。为了清晰起见,我保持搜索功能简单,但可以使用过滤器和闭包缩短代码。

import UIKit

class Contact  {

      var name: String?
      var companyName: String?
      var phoneNumbers: [String]?
      var addresses: [String]?

      init (name: String, companyName: String, phoneNumbers: [String], addresses: [String]) {
            self.name = name
            self.companyName = companyName
            self.phoneNumbers = phoneNumbers
            self.addresses = addresses
      }
}

var contacts: [Contact] =
      [(Contact.init(name: "Tom Jones", companyName: "Jones", phoneNumbers: ["111-1111", "111-1112", "111-1113"], addresses: ["Hollywood", "New York"])),
       (Contact.init(name: "Aretha Franklin", companyName: "Franklin", phoneNumbers: ["111-1114", "111-1115", "111-1116"], addresses: ["Detroit", "Hollywood"])),
       (Contact.init(name: "Axel Rose", companyName: "Rose", phoneNumbers: ["111-1117", "111-11128", "111-1111"], addresses: ["London", "New York"])) ]

let phoneNumberSearchString = "111-1111"

func searchByPhoneNumber (searchNumber: String, inDataSet contacts: [Contact]) -> [Contact] {
      var searchResult: [Contact] = []
      for contact in contacts {
            for phoneNumber in contact.phoneNumbers! {
                  if phoneNumber == searchNumber {
                        searchResult.append(contact)
                  }
            }
      }
      return searchResult
}

func searchByAddress (searchAddress: String, inDataSet contacts: [Contact]) -> [Contact] {
      var searchResult: [Contact] = []
      for contact in contacts {
            for address in contact.addresses! {
                  if address == searchAddress {
                        searchResult.append(contact)
                  }
            }
      }
      return searchResult
}

let foundContacts = searchByPhoneNumber("111-1111", inDataSet: contacts)

for contact in foundContacts {
      print(contact.name!)
}

let foundContacts2 = searchByAddress("Hollywood", inDataSet: contacts)

for contact in foundContacts2 {
      print(contact.name!)
}

/* Print results

 Tom Jones
 Axel Rose
 Tom Jones
 Aretha Franklin

 */