简体   繁体   中英

Converting objective-c method to swift [updateSearchResultsForSearchController]

I'm trying to convert the following Objective-C method to Swift, but I've came across a few issues.

I'm having trouble using the Predicate as it's described below to the Swift syntax.

Can someone give me a hint?

- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {

// filter the search results
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains [cd] %@", self.controller.searchBar.text];
self.results = [self.data filteredArrayUsingPredicate:predicate];

// NSLog(@"Search Results are: %@", [self.results description]);

}

Regards, IC

let predicate = NSPredicate(format: "SELF contains[cd] %@", self.controller.searchBar.text!)
results = data.filter({ item in
    predicate.evaluateWithObject(item)
})

Or for a more concise solution:

let predicate = NSPredicate(format: "SELF contains[cd] %@", self.controller.searchBar.text!)
results = data.filter { predicate.evaluateWithObject($0) }

Try this:

func updateSearchResultsForSearchController(searchController: UISearchController) {
    let predicate = NSPredicate(format: "SELF contains [cd] %@", argumentArray: [self.controller.searchBar.text])
    self.results = (self.data as NSArray).filteredArrayUsingPredicate(predicate)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM