ios - 类型 'NSPredicate has no subscript members'

标签 ios swift search

我正在尝试实现一个将其结果调用到数据库的搜索。我设法在控制台中打印数据库,但无法通过搜索访问它。

这是我使用的代码:

import UIKit

class SearchTableViewController: UITableViewController, UISearchResultsUpdating {

var searchPredicate = NSPredicate()
var filteredAppleProducts = [String]()
var resultSearchController = UISearchController!()
var arrDataArticles: NSMutableArray!

override func viewDidLoad() {
    super.viewDidLoad()

    self.searchPredicate = NSPredicate(format: "SELF LIKE[c] %@", resultSearchController.searchBar.text!)

    self.resultSearchController = UISearchController(searchResultsController: nil)
    self.resultSearchController.searchResultsUpdater = self

    self.resultSearchController.dimsBackgroundDuringPresentation = false
    self.resultSearchController.searchBar.sizeToFit()

    self.tableView.tableHeaderView = self.resultSearchController.searchBar

    self.tableView.reloadData()
    self.getAllArticles()
}

func getAllArticles() {
    arrDataArticles = NSMutableArray()
    arrDataArticles = ModelBD.getInstance().getAllArticles()
}

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

// MARK: - Table view data source

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    if self.resultSearchController.active {
        return self.filteredAppleProducts.count
    }
    else {
        return 0
    }
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell?

    if self.resultSearchController.active {
        cell!.textLabel?.text = self.filteredAppleProducts[indexPath.row]
    }
    else {
        cell!.textLabel?.text = self.searchPredicate[indexPath.row] as! String
    }

    return cell!
}

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

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

    let array = (self.appleProducts as NSArray).filteredArrayUsingPredicate(searchPredicate)

    self.filteredAppleProducts = array as! [String]

    self.tableView.reloadData()
}


}

Type 'NSPredicate has no subscript members'

行上出现错误

cell!.textLabel?.text = self.searchPredicate[indexPath.row] as! String

let array = (self.appleProducts as NSArray).filteredArrayUsingPredicate(searchPredicate)

有人知道如何解决这个问题吗?

预先感谢您的宝贵时间。

最佳答案

您不能在这样的常量中使用实例成员。在您的谓词中,您已经假设存在 resultSearchController。相反,您可以在 viewDidLoad() 中或在类初始化期间初始化谓词。

例如:

var searchPredicate: NSPredicate?

override func viewDidLoad() {
    super.viewDidLoad()
    self.searchPredicate = NSPredicate(format: "SELF LIKE[c] %@", resultSearchController.searchBar.text!)

[...]

关于ios - 类型 'NSPredicate has no subscript members',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38829871/

相关文章:

ios - 秒表和计时器

ios - 如何创建 react native polyfill?

ios - 在不损失质量的情况下在 Objective-C 中截取屏幕截图

ios - 使用 Swift 更新 Firebase 中未读消息的数量

ios - 使用 QuincyKit 符号化崩溃锁不符号化

ios - 我应该如何子类化它?

xcode - 使用 Swift 的预期声明错误

python - 从 Python 库的角度来看,爬行、解析、索引、搜索之间有什么区别

google-maps - 谷歌地图 API V3 : search markers

javascript - 如何在 node.js 中搜索流中的字符串?