nspredicate

Show distinct results in fetch request, group by an attribute and calculate the total for that attribute

本秂侑毒 提交于 2019-12-20 02:00:14
问题 Scenario: I have an expense tracking iOS Application and I have a view controller called "DashBoardViewController" (table view controller - with FRC) which would basically categorize my expenses/incomes for a given week, a month, or year and display it as the section header title for example : (Oct 1- Oct 7, 2012) and it shows expenses/incomes ROWS and related stuff according to that particular week or month or year. My Question: What I want to accomplish is : Suppose I save 3 new expenses

NSPredicate with boolean is not comparing a boolean to NO

混江龙づ霸主 提交于 2019-12-19 12:38:19
问题 I'm using CoreData and my entity has a boolean property called "isReward". I'm trying to fetch the entity and filter out all results where isReward == YES. isReward != YES does not work. isReward == NO does not work. NSPredicate predicateWithFormat:"isReward != %@", [NSNumber numberWithBool:YES]] does not work isReward == Nil works. What gives? 回答1: I'm guessing you'll want: [NSPredicate predicateWithFormat:@"isReward = NO OR isReward = nil"] (Per the Apple docs here, direct comparison to NO

NSPredicate with boolean is not comparing a boolean to NO

自作多情 提交于 2019-12-19 12:38:13
问题 I'm using CoreData and my entity has a boolean property called "isReward". I'm trying to fetch the entity and filter out all results where isReward == YES. isReward != YES does not work. isReward == NO does not work. NSPredicate predicateWithFormat:"isReward != %@", [NSNumber numberWithBool:YES]] does not work isReward == Nil works. What gives? 回答1: I'm guessing you'll want: [NSPredicate predicateWithFormat:@"isReward = NO OR isReward = nil"] (Per the Apple docs here, direct comparison to NO

Core Data Transformable NSArray of IDs

旧城冷巷雨未停 提交于 2019-12-19 11:27:49
问题 I have an CoreData entity class X, which stores an NSArray of NSString IDs, inside a transformable attribute (lets say someIDs)... Given an NSString ID, how can i find (using Predicate) all entities X, which contains the ID, in their someIDs attribute? Note: i know about relationships (so no need to propose using a relationship) etc. but what to do in this case? 回答1: You can't. A transformable attribute is store as a data blob in the SQLite store file (using NSCoding methods). The Core Data

Find values in NSArray having NSArray with NSString using NSPredicate iOS

感情迁移 提交于 2019-12-19 04:14:50
问题 With NSArray only i can find values as: NSArray *arr = [NSArray arrayWithObjects:@"299-1-1", @"299-2-1", @"299-3-1", @"399-1-1", @"399-2-1", @"399-3-1", @"499-1-1", @"499-2-1", @"499-3-1", nil]; NSString *search = @"299"; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS %@",[NSString stringWithFormat:@"%@", search]]; NSArray *array = [arr filteredArrayUsingPredicate: predicate]; NSLog(@"result: %@", array); Found Result as expected : result: ( "299-1-1", "299-2-1",

iOS 11 NSPredicate search on Swift array crashing - NSUnknownKeyException

隐身守侯 提交于 2019-12-19 02:54:11
问题 I am using NSPredicate to filter an array in Swift. The problem is after updating to iOS 11 (Xcode 9 /w Swift 4), I keep getting a crash on the filter line. Here is the crash log: Terminating app due to uncaught exception 'NSUnknownKeyException', reason: >'[ valueForUndefinedKey:]: this class is not key >value coding-compliant for the key name.' Here is an example of the class that I have an array of: final class Model: NSObject { let name: String init(name: String) { self.name = name } }

Remove objects using NSPredicate

こ雲淡風輕ζ 提交于 2019-12-18 18:30:55
问题 I have the folloving dictionary which has many sub dictionaries. How can I remove objects where isChanged = 1 from parent dictionary using NSPredicate ? { "0_496447097042228" = { cellHeight = 437; isChanged = 1; }; "100000019882803_193629104095337" = { cellHeight = 145; isChanged = 0; }; "100002140902243_561833243831980" = { cellHeight = 114; isChanged = 1; }; "100004324964792_129813607172804" = { cellHeight = 112; isChanged = 0; }; "100004324964792_129818217172343" = { cellHeight = 127;

Using NSPredicate with Core Data for deep relationships

北城余情 提交于 2019-12-18 11:59:56
问题 I have an NSArrayController, companiesController bound to a top level Core Data entity, Companies . A Company has many Department 's, and a Department has many Employee ; these are represented by the 1-to-many relationships, departments and employees . Based on the attribute salary of an Employee I thought I could dynamically do this for filtering based on salary inside a UI-called method: NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY departments.employees.salary < %@",

Filtering NSArray of NSDictionary objects using NSPredicate

删除回忆录丶 提交于 2019-12-18 10:31:51
问题 I have an NSArray of NSDictionary objects. I want to filter the array based on keys of the dictionaries using NSPredicate. I have been doing something like this: NSString *predicateString = [NSString stringWithFormat:@"%@ == '%@'", key, value]; NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateString]; NSArray *filteredResults = [allResultsArray filteredArrayUsingPredicate:predicate]; This works fine if they key passed in is one-word: Color, Name, Age. But it doesn't work if

CoreData predicate: string property length?

可紊 提交于 2019-12-18 08:09:10
问题 Say if I have an entity Fragment , it has an attribute 'text' which is a string, I want to query the list of Fragment whose text is of length 5: [NSPredicate predicateWithFormat:@"position == %@ AND text.length == %d", pos, 5]; It does not work (ie returns no result), but if I remove text.length in the query it works and I'm certain that there are texts of length 5, so what do I need to change it to? Thanks! 回答1: There is no length attribute for strings in the NSPredicate . Use regex instead.