Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Predicate in Swift

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):

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope {     NSPredicate *resultPredicate = [NSPredicate predicateWithFormat:@"name contains[c]         %@", searchText];     searchResults = [recipes filteredArrayUsingPredicate:resultPredicate]; } 

Can anyone advise how to create an equivalent for NSPredicate in Swift?

like image 313
levitatejay Avatar asked Jun 12 '14 04:06

levitatejay


People also ask

What is predicate in Swift?

Predicates represent logical conditions, which you can use to filter collections of objects.

What is predicate in Core Data?

A CoreData predicate has a simple format attribute == value which is very similar to aContact.uniqueId! == contactIdentifierString : var contactIdentifierString = "" func userSelectedContact(contactIdentifier: String) { let context = (UIApplication.shared.delegate as!

How do you write NSPredicate in Swift?

In Swift, constructors skip the "blahWith…" part and just use the class name as a function and then go straight to the arguments, so [NSPredicate predicateWithFormat: …] would become NSPredicate(format: …) . (For another example, [NSArray arrayWithObject: …] would become NSArray(object: …) .


2 Answers

This is really just a syntax switch. OK, so we have this method call:

[NSPredicate predicateWithFormat:@"name contains[c] %@", searchText]; 

In Swift, constructors skip the "blahWith…" part and just use the class name as a function and then go straight to the arguments, so [NSPredicate predicateWithFormat: …] would become NSPredicate(format: …). (For another example, [NSArray arrayWithObject: …] would become NSArray(object: …). This is a regular pattern in Swift.)

So now we just need to pass the arguments to the constructor. In Objective-C, NSString literals look like @"", but in Swift we just use quotation marks for strings. So that gives us:

let resultPredicate = NSPredicate(format: "name contains[c] %@", searchText) 

And in fact that is exactly what we need here.

(Incidentally, you'll notice some of the other answers instead use a format string like "name contains[c] \(searchText)". That is not correct. That uses string interpolation, which is different from predicate formatting and will generally not work for this.)

like image 77
Chuck Avatar answered Sep 19 '22 15:09

Chuck


Working with predicate for pretty long time. Here is my conclusion (SWIFT)

//Customizable! (for me was just important if at least one) request.fetchLimit = 1   //IF IS EQUAL  //1 OBJECT request.predicate = NSPredicate(format: "name = %@", txtFieldName.text)  //ARRAY request.predicate = NSPredicate(format: "name = %@ AND nickName = %@", argumentArray: [name, nickname])   // IF CONTAINS  //1 OBJECT request.predicate = NSPredicate(format: "name contains[c] %@", txtFieldName.text)  //ARRAY request.predicate = NSPredicate(format: "name contains[c] %@ AND nickName contains[c] %@", argumentArray: [name, nickname]) 
like image 20
Jiří Zahálka Avatar answered Sep 20 '22 15:09

Jiří Zahálka