nsexpression

Swift 4 CoreData NSFetchRequest: sum over computed variable

那年仲夏 提交于 2021-01-28 08:50:41
问题 I'm using CoreData for persistence and would like to use a NSFetchRequest with an NSExpression(forFunction: "sum:" ...). I have three Entities Entity A: name: String value: Int64 Entity B: name: String Entity C: relationship1: A relationship2: B value: Int64 I have a separate Entity for a relationship between an A- and an B-Entity, because a relationship also has an Int64 Value. I guess this is the easiest solution not to sum up one attribute of a given set of Cs (using a NSPredicate to

How to get floating point number from the NSExpression's expressionValueWithObject:context method?

て烟熏妆下的殇ゞ 提交于 2020-01-24 04:32:46
问题 I have already implemented a custom calculator where I am using following code to evaluate the arithmetic expression something like 5+3*5-3. - (NSNumber *)evaluateArithmeticStringExpression:(NSString *)expression { NSNumber *calculatedResult = nil; @try { NSPredicate * parsed = [NSPredicate predicateWithFormat:[expression stringByAppendingString:@" = 0"]]; NSExpression * left = [(NSComparisonPredicate *)parsed leftExpression]; calculatedResult = [left expressionValueWithObject:nil context:nil

Fetch aggregate data from NSManagedObject using another expression as argument to sum: expression

怎甘沉沦 提交于 2020-01-10 03:15:28
问题 Is it possible to use expression as argument for sum: expression? I have entity ( NSManagedObject class) Drink with properties costPerDrink , numberOfDrinks and drinkingDate . I would like to get sum of total costs ( numberOfDrinks multiplied by costPerDrink ) within time period. While I have no problems getting sum on single property (e.g. sum of numberOfDrinks within time period), when I try to use sum expression on another (multiply) expression I got error: NSInvalidArgumentException,

Can I force NSExpression and expressionValue to assume Doubles instead of Ints somehow?

天涯浪子 提交于 2020-01-09 08:12:18
问题 I'm trying to do math from a string. When I turn a string into a math problem with NSExpression, and then get the result with expressionValue, Swift assumes I want an Integer. Consider these two Playground examples: let currentCalculation = "10 / 6" let currentExpression = NSExpression(format: currentCalculation) print(currentExpression) // 10 / 6 if let result = currentExpression.expressionValue(with: nil, context: nil) as? Double { print(result) // 1 } let anotherCalculation = "10.0 / 6.0"

Can I force NSExpression and expressionValue to assume Doubles instead of Ints somehow?

坚强是说给别人听的谎言 提交于 2020-01-09 08:11:33
问题 I'm trying to do math from a string. When I turn a string into a math problem with NSExpression, and then get the result with expressionValue, Swift assumes I want an Integer. Consider these two Playground examples: let currentCalculation = "10 / 6" let currentExpression = NSExpression(format: currentCalculation) print(currentExpression) // 10 / 6 if let result = currentExpression.expressionValue(with: nil, context: nil) as? Double { print(result) // 1 } let anotherCalculation = "10.0 / 6.0"

Calculate lessthan value using NSExpression?

混江龙づ霸主 提交于 2019-12-29 09:33:26
问题 For the following block of code: NSString *formul=@"5 < 9"; NSExpression *e = [NSExpression expressionWithFormat:formul]; int result = [[e expressionValueWithObject:nil context:nil] intValue]; NSLog(@"formule:%d", result); I got error: due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "5 < 9 == 1"' 回答1: You can use NSPredicate instead: NSString *formul = @"15 < 9"; NSPredicate *predicate = [NSPredicate predicateWithFormat:formul]; BOOL b =

NSExpression based Core Data fetch does not retrieve current values (iOS 5, GCD)

ⅰ亾dé卋堺 提交于 2019-12-22 07:24:12
问题 What I am trying to do I am using the code below to download data (historic foreign exchange rates) from my backend server (parse.com) to my app's Core Data store. The app checks for the latest available data stored locally and fetches only the newer data from the server. If there is no data stored locally yet, it fetches all data from the server. The way the code is set up, it fetches the data in batches of 100 objects, saves the objects in Core Data, gets the new latest date for which data

NSFetchRequest for groupby and count combination

不打扰是莪最后的温柔 提交于 2019-12-20 07:11:56
问题 I'm new in Core Data, so i want to write simple fetch request to group result by some field and also a count of another field in that group. so for example i have products table i want to retrieve all grouped products by date and the count of product, the simple query in sql is like SELECT DateRegistered,Count(*) FROM Products Group By DateRegistered 回答1: Your best bet is the very flexible NSFetchedResultsController . You would have a fetch request that simply fetches the Product entity

Correct way to catch NSInvalidArgumentException when using NSExpression [duplicate]

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-19 09:09:30
问题 This question already has answers here : Catch an exception for invalid user input in swift (3 answers) Closed 2 years ago . I want to validate user created expressions (like "2+2", "5+7" or more complex). I use NSExpression class to parse and calculate this expressions. This is my Playground code: import UIKit let string = "2+2" var ex:NSExpression? do { ex = NSExpression(format: string) } catch { print("String is not valid expression") } if let result = ex?.expressionValue(with: nil,

iOS FetchRequest on aggregate functions: How to include pending changes?

非 Y 不嫁゛ 提交于 2019-12-14 01:32:43
问题 I've finally at least narrowed down this problem. I'm computing some aggregate functions (as in this example the sum) of expenditures. If I change some expenditures, this aggregate fetch doesn't refresh immediately but only after a while (probably after the changes have been saved to the database). I've found this part in the doc: - (void)setIncludesPendingChanges:(BOOL)yesNo As per the documentation A value of YES is not supported in conjunction with the result type NSDictionaryResultType ,