nspredicate

Updating fetchedResultsController for predicate set by UISearchBar

淺唱寂寞╮ 提交于 2019-12-04 17:27:01
Refactored to vanilla UIViewController Rather than having the UITableView toggle between active and inactive UISearchController , I refactored to a vanilla UIViewController with a UISearchBar at the top and a UITableView directly underneath it. What I'm trying to do is toggle the fetchedResultsController between having a predicate which is set by the text in the searchBar and one that grabs everything. I found this answer, which describes a solution to a similar problem in Objective-C and served as the basis upon which I refactored my Swift project: How to filter NSFetchedResultsController

NSFetchedResultsController and constructing NSFetchRequests

这一生的挚爱 提交于 2019-12-04 16:59:59
I have setup Core Data for an iPhone app without an instance of NSFetchedResultsController. To do this, I created a created a model class to encapsulate all core data requests and constructing of NSFetchRequests/NSPredicates. This kept all Core Data specific code out of my UITableViewController. Now I want to add NSFetchedResultsController to make populating a sectioned UITableView easier. My issue is this: In all examples I have seen the instance NSFetchedResultsController is an ivar of the UITableViewController. This leads to all NSFetchedResults statements constructed within the

NSPredicate - Unable to parse the format string [duplicate]

那年仲夏 提交于 2019-12-04 15:49:45
问题 This question already has answers here : Creating NSPredicate dynamically by setting the key programmatically (2 answers) Closed 6 years ago . I am trying to write a NSPredicate to fetch rows with my_column value with this string " 193e00a75148b4006a451452c618ccec " and I get the below crash. Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "my_column=193e00a75148b4006a451452c618ccec"' My predicate statement fetchRequest

NSPredicate versus NSString: Which is better/faster for finding superstrings?

一笑奈何 提交于 2019-12-04 14:31:44
问题 I have a large number of strings that I'm searching to see if a given substring exists. It seems there are two reasonable ways to do this. Option 1: Use the NSString method rangeOfSubstring and test whether .location exists: NSRange range = [string rangeOfSubstring:substring]; return (range.location != NSNotFound); Option 2. Use the NSPredicate syntax CONTAINS : NSPredicate *regex = [NSPredicate predicateWithFormat:@"SELF CONTAINS %@", substring]; return ([regex evaluateWithObject:string] ==

Pass the key to predicate as parameter in predicateWithFormat

荒凉一梦 提交于 2019-12-04 11:27:03
This may sounds silly, but here is the data to predicate: @interface Person @property NSString *name; @property NSString *phone; @property NSString *address; @end I got an NSPredicate that will search through Person Array, search by Name. Normally, I will use it like NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@"name contains[c] %@", searchText]; But how can I use the key (name) as a parameter? Like NSString *key = @"name"; NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"%@ contains[c] %@", key, searchText]; The predicate1 works fine, but the predicate2 doesn't. The

NSPredicate - filtering values based on a BOOLEAN stored value

∥☆過路亽.° 提交于 2019-12-04 08:49:31
问题 I have a core data model object called Entry. In this I have an attribute IsFavorite. I would like to use an NSPredicate to filter the results of my NSFetchedResultsController. Currently I am getting EXC_BAD_ACCESS when the fetch executes. NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; // Edit the entity name as appropriate. NSEntityDescription *thisEntry = [NSEntityDescription entityForName:@"Entry" inManagedObjectContext:managedObjectContext_]; [fetchRequest setEntity

CoreData: Find minimum of calculated property

China☆狼群 提交于 2019-12-04 08:47:14
问题 Say I have a CoreData entity "Point" with two properties x and y (both NSNumber). How would a NSPredicate need to look like to let me find the closest Point to say a,b? for distance = sqrt((x-a) (x-a)+(y-b) (y-b)) While I could define a transient property that calculates a distance to a predefined point I can't see how I could programmatically change that point when launching a fetchrequest. Any help would be greatly appreciated. 回答1: Sorry, but doing arithmetic in an NSPredicate isn't

NSPredicate - case insensitive filtering for multiple conditions

不羁的心 提交于 2019-12-04 08:29:41
I'm using following NSPredicate for filtering, NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(firstName CONTAINS %@ ) OR (lastName CONTAINS %@ )OR (UserName CONTAINS %@ ) ", myText,myText,myText]; NSArray *filtered = [responseArray filteredArrayUsingPredicate:predicate]; It works fine, but it's case-sensitive. I need the filtering should be case insensitive. I've tried, NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(ANY firstName CONTAINS %@ ) OR (ANY lastName CONTAINS %@ )OR (ANY UserName CONTAINS %@ ) ", myText,myText,myText]; But it throws the error ***

NSPredicate unable to parse format string

耗尽温柔 提交于 2019-12-04 08:20:17
I can't see any problem with these line of code. Why can't it parse? [NSPredicate predicateWithFormat:@"(username CONTAINS[cd] %1$@) || " "(userId CONTAINS[cd] %1$@) || " "(firstname CONTAINS[cd] %1$@) || " "(lastname CONTAINS[cd] %1$@)", searchString]" The Log doesn't help either. Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "(username CONTAINS[cd] %1$@) || (userId CONTAINS[cd] %1$@) || (firstname CONTAINS[cd] %1$@) || (lastname CONTAINS[cd] %1$@)"' Edit 1: Okay, it seems like predicateWithFormat doesn't understand "%1$@".

NSPredicate with a !=?

瘦欲@ 提交于 2019-12-04 06:25:21
问题 I have Core Data Entities Person and Boundary. They have a many-to-many relationship (each person can have many boundaries, and each boundary can have many persons). I am trying to create a list of what boundaries Person Fred doesn't have a relationship too. Person *person = [Person MR_findFirstByAttribute:@"name" withValue:@"Fred"]; DLog(@"person.boundaries.count: %d", person.boundaries.count); NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY persons != %@", person]; DLog(@