nspredicate

Core Data ANY BETWEEN predicate

做~自己de王妃 提交于 2019-12-03 13:49:28
问题 I'm trying to create an NSPredicate to find 'projects' that contain 'sessions' within a certain date range. I tried this at first: [NSPredicate predicateWithFormat:@"ANY sessions.date BETWEEN {$STARTDATE, $ENDDATE}"]; But I get an exception: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'to-many key not allowed here' It seems BETWEEN does not work with ANY in this way. I'm also limited in use of () and AND clauses meaning I can't use something like:

fetch request for entity.attribute == @“somevalue”

折月煮酒 提交于 2019-12-03 13:28:33
How do I setup a fetch request to only pull the data from an entity's attribute with one particular value? This is the basic code I've used before. -(void)fetchResults { NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:self.entityDescription.name]; NSString *cacheName = [self.entityDescription.name stringByAppendingString:@"Cache"]; // predicate code if (self.predicate != nil) { [fetchRequest setPredicate:self.predicate]; } // end of predicate code NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]; [fetchRequest

Core Data Transformable attributes NOT working with NSPredicate

删除回忆录丶 提交于 2019-12-03 13:06:20
I often use Transformable for Core Data attributes , so I can change them later. However, it seems like, if I want to use NSPredicate to find a NSManagedObject , using "uniqueKey == %@" , or "uniqueKey MATCHES[cd] %@" , it's not working as it should. It always misses matching objects, until I change the attributes of the uniqueKey of the matching object to have specific class like NSString , or NSNumber . Can someone explain the limitation of using NSPredicate with Transformable attributes? Note : I'm not sure when/if this has changed since 5/2011 (from Scott Ahten's accepted answer), but you

NSPredicate 'OR' filtering based on an NSArray of keys

霸气de小男生 提交于 2019-12-03 13:01:13
Consider the following NSArray: NSArray *dataSet = [[NSArray alloc] initWithObjects: [NSDictionary dictionaryWithObjectsAndKeys:@"abc", @"key1", @"def", @"key2", @"hij", @"key3", nil], [NSDictionary dictionaryWithObjectsAndKeys:@"klm", @"key1", @"nop", @"key2", nil], [NSDictionary dictionaryWithObjectsAndKeys:@"qrs", @"key2", @"tuv", @"key4", nil], [NSDictionary dictionaryWithObjectsAndKeys:@"wxy", @"key3", nil], nil]; I am able to filter this array to find dictionary objects that contain the key key1 // Filter our dataSet to only contain dictionary objects with a key of 'key1' NSString *key =

Creating NSPredicate dynamically by setting the key programmatically

旧城冷巷雨未停 提交于 2019-12-03 12:47:34
问题 Why does the former of following snippets work while not the latter ? Snippet 1 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(coin_unique == %@)", [NSNumber numberWithInt:species]]; Snippet 2 // Does NOT Work NSString *predicateText = @"coin_unique"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(%@ == %@)", predicateText, [NSNumber numberWithInt:species]]; I have to dynamically create predicate depending upon the argument received in my method. 回答1: coin_unique

NSPredicate find object with attribute in NSSet

£可爱£侵袭症+ 提交于 2019-12-03 12:44:26
I have an NSMangedObject that contains NSSet of other NSManagedObjects . I need to check if these objects has an value in NSSet and then return them. I use MagicalRecord for fetching data. So I need something like this: NSPredicate *predicate = [NSPredicate predicateWithFormat:@"stringObjects contains %@", string]; So if NSSet stringObjects contains some string that I am looking for so then return object I have requested. One note here: stringObjects (name just for example it represent my NSSet ) it is NSSet that contains NSManagedObjects, so I need to search them by some id (for example

Core Data- predicate with dates

为君一笑 提交于 2019-12-03 12:06:26
I'm stumped trying to write a predicate for "Recently Completed" tasks, i.e. show the task if it was completed within the last 7 days. I think I need to do something like this: "if NOW < dateCompleted + 7 days". The dateCompleted is an attribute on the table, but I'm not sure how I'm supposed to get it's value and add 7 days to it from within the predicate. I guess I need to fetch the attribute value first before writing the NSPredicate, but how? I don't have access to the managedObject at this point. This might be close the solution, but I can't figure out how to define 'oneWeek' and I don't

Error: 'Unsupported predicate in fetch options: mediaType == 2'

狂风中的少年 提交于 2019-12-03 11:39:27
问题 I'm trying to use smartAlbum to generate an array of either only videos or only photos or both. You can see my code below: PHFetchResult *collectionList = [PHCollectionList fetchMomentListsWithSubtype:PHCollectionListSubtypeMomentListCluster options:nil]; PHFetchOptions *options = nil; if (self.xSelected) { options = [[PHFetchOptions alloc] init]; options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]]; options.predicate = [NSPredicate

Regex for an email address doesn't work

只愿长相守 提交于 2019-12-03 11:21:35
问题 I'm trying to check if some email address is correct with the following code : NSPredicate *regexMail = [NSPredicate predicateWithFormat:@"SELF MATCHES '.*@.*\..*'"]; if([regexMail evaluateWithObject:someMail]) ... But the "\." doesn't seem to work since the mail "smith@company" is accepted. Besides, I have the following warning : "Unknown escape sequence" Edit : I'm programming in Objective-C for iPhone. Thanks 回答1: You cannot correctly validate an email address with regular expressions

Shorter way to write NSPredicate format string for testing multiple properties?

匿名 (未验证) 提交于 2019-12-03 10:24:21
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: Is there a shorter way to write the format string for a predicate equivalent to this: [ NSPredicate predicateWithFormat : @ "key1 CONTAINS[cd] %@ OR key2 CONTAINS[cd] %@ OR key3 CONTAINS[cd] %@" , searchString , searchString , searchString ]; I have written a few predicate format strings like this, and I was thinking to simplify that by writing a method that takes an array of key paths and the search string to construct such a predicate. But I thought I’d ask if there is a built-in way to do this, before doing that. 回答1: