Swift-使用UISearchController和NSPredicate和结构数组进行搜索 [英] Swift - Search using UISearchController and NSPredicate with an array of structs

查看:110
本文介绍了Swift-使用UISearchController和NSPredicate和结构数组进行搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在尝试为tableView设置搜索栏。我想用NSPredicate过滤用户的输入以显示来自结构对象数组的过滤数据。

Currently I'm trying to set up a search bar for my tableView. I want to filter the user's input with NSPredicate to display filtered data from an array of struct objects.

但是当我尝试调用 .filteredArrayUsingPredicate( )在这样的数组上,出现以下错误 [Course]无法转换为'NSArray'

But when I try to call .filteredArrayUsingPredicate() on such an array, I get the following error [Course] is not convertible to 'NSArray'.

下面是我的代码和指向存储库的链接。我也欢迎任何更好的方法。

Below is my code and a link to the repo. I'm also open to any better methods of approach.

import UIKit
import SwiftyJSON

class CourseTableViewController: UITableViewController, UISearchResultsUpdating {

    var allCourses: [Course] = [Course]()

    var searchArray: [Course] = [Course]() {
        didSet {
            self.tableView.reloadData()
        }
    }

    var resultSearchController = UISearchController()

    override func viewDidLoad() {
        super.viewDidLoad()
        retrieveCourses()
        configureView()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func configureView() {
        tableView.rowHeight = 50

        // Search Controller Initialization
        self.resultSearchController = UISearchController(searchResultsController: nil)
        resultSearchController.searchResultsUpdater = self
        resultSearchController.hidesNavigationBarDuringPresentation = false
        resultSearchController.dimsBackgroundDuringPresentation = false
        resultSearchController.searchBar.searchBarStyle = .Minimal
        resultSearchController.searchBar.sizeToFit()
        self.tableView.tableHeaderView = resultSearchController.searchBar
    }

    func retrieveCourses() {
        APIService.getAllCourses() { (data) -> Void in
            for courseIndex in data {
                var course: Course = Course(courseJSON: courseIndex.1)
                self.allCourses.append(course)
                println("Course Index: " + String(self.allCourses.count))
            }

            self.sortAllCourses()
        }
    }

    func sortAllCourses() {
        allCourses.sort() {$0.abbr < $1.abbr}
        self.tableView.reloadData()
    }

    // MARK: - Search

    func updateSearchResultsForSearchController(searchController: UISearchController) {
        self.searchArray.removeAll(keepCapacity: false)

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

        // ERROR is on this line
        let filteredArray: Array = (allCourses as NSArray).filteredArrayUsingPredicate(searchPredicate)

        self.searchArray = filteredArray as [Course]
    }

    // Full tableView code can be found in the repo
}

链接: https://github.com/classmere/app / tree / feature / issue / 1 / implementSearch

非常感谢!

推荐答案

所以,在四处张望之后,我发现解决这种情况的方法是简单地使用.filter()和.rangeOfString()解决此问题。

So after much looking around, I found that a better approach for a situation like this would be to simply use .filter() and .rangeOfString() to solve this

theArray.filter() { $0.json?.rangeOfString(searchQuery, options: .CaseInsensitiveSearch) != nil}

虽然不使用NSPredicate,但对于我当前的设置,这似乎更容易实现。

While NSPredicate is not used, this appears to be a much easier implementation for my current setup.

这篇关于Swift-使用UISearchController和NSPredicate和结构数组进行搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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