Using Predicate in Swift

后端 未结 8 902
陌清茗
陌清茗 2020-12-07 12:16

I\'m working through the tutorial here (learning Swift) for my first app: http://www.appcoda.com/search-bar-tutorial-ios7/

I\'m stuck on this part (Objective-C code)

相关标签:
8条回答
  • 2020-12-07 12:45

    In swift 2.2

    func filterContentForSearchText(searchText: String, scope: String) {
        var resultPredicate = NSPredicate(format: "name contains[c]         %@", searchText)
        searchResults = (recipes as NSArray).filteredArrayUsingPredicate(resultPredicate)
    }
    

    In swift 3.0

    func filterContent(forSearchText searchText: String, scope: String) {
            var resultPredicate = NSPredicate(format: "name contains[c]         %@", searchText)
            searchResults = recipes.filtered(using: resultPredicate)
        }
    
    0 讨论(0)
  • 2020-12-07 12:50

    Example how to use in swift 2.0

    let dataSource = [
        "Domain CheckService",
        "IMEI check",
        "Compliant about service provider",
        "Compliant about TRA",
        "Enquires",
        "Suggestion",
        "SMS Spam",
        "Poor Coverage",
        "Help Salim"
    ]
    let searchString = "Enq"
    let predicate = NSPredicate(format: "SELF contains %@", searchString)
    let searchDataSource = dataSource.filter { predicate.evaluateWithObject($0) }
    

    You will get (playground)

    0 讨论(0)
提交回复
热议问题