核心数据搜索谓词不在谓词中搜索符号

时间:2015-09-07 06:47:23

标签: swift core-data

尝试在我的应用程序中实现搜索功能。不包含符号的普通卡片名称的基本搜索功能可以使用但是,我有一些卡片的名称包含不会搜索的符号,即使您将相应的卡片名称包含在这些符号中也是如此。当文本字段为空时,尝试使用它也有一些轻微的困难,完整列表返回。我实施的搜索是:

func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {

    searchBar.setShowsCancelButton(true, animated: true)

    let requiredClassName = passedDeckObject!.classSelected.classname
    var requiredCardClasses = [Int]()
    switch(requiredClassName) {
    case "Warrior":
        requiredCardClasses = [1,10]
    case "Shaman":
        requiredCardClasses = [2,10]
    case "Rogue":
        requiredCardClasses = [3,10]
    case "Paladin":
        requiredCardClasses = [4,10]
    case "Hunter":
        requiredCardClasses = [5,10]
    case "Druid":
        requiredCardClasses = [6,10]
    case "Warlock":
        requiredCardClasses = [7,10]
    case "Mage":
        requiredCardClasses = [8,10]
    case "Priest":
        requiredCardClasses = [9,10]
    default:
        requiredCardClasses = [10]
    }


    if !searchText.isEmpty {
        // Clear out the fetchedResultController

        // Setup the fetch request
        let fetchRequest = NSFetchRequest(entityName: "Cards")
        fetchRequest.fetchLimit = 50
        fetchRequest.predicate = NSCompoundPredicate(type: .AndPredicateType, subpredicates: [NSPredicate(format: "name contains[cd] %@", searchText), NSPredicate(format: "cardClass IN %@", requiredCardClasses)])
        let sortDescriptor = NSSortDescriptor(key: "name", ascending: true)
        fetchRequest.sortDescriptors = [sortDescriptor]
        //end//


        //cost fetch request begins here//
        let costfetchRequest = NSFetchRequest(entityName: "cardCost")
        costfetchRequest.fetchLimit = 50
        costfetchRequest.predicate = NSPredicate(format: "cardCost = %i", searchText)
        let costSortDescriptor = NSSortDescriptor(key: "cardCost", ascending: true)
        costfetchRequest.sortDescriptors = [sortDescriptor]
        //end//


        // Pass the fetchRequest and the context as parameters to the fetchedResultController
        frc = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext:managedObjectContext!,
            sectionNameKeyPath: nil, cacheName: nil)
        // Make the fetchedResultController a delegate of the MoviesViewController class
        frc.delegate = self
        // Execute the fetch request or display an error message in the Debugger console
        var error: NSError? = nil
        if (!frc.performFetch(&error)) {
            println("Error: \(error?.localizedDescription)")
        }
        // Refresh the table view to show search results
        cardsListed.reloadData()
    }
}

0 个答案:

没有答案