我尝试将以下NSPredicate
与NSDecimalNumber
一起使用来检查值是否介于最小值和最大值之间:
NSNumber *deltaNumber = [[NSUserDefaults standardUserDefaults] valueForKey: kMyDeltaValue];
NSDecimalNumber *delta = [NSDecimalNumber decimalNumberWithDecimal: [deltaNumber decimalValue]];
NSDecimalNumber* min = [NSDecimalNumber decimalNumberWithString: searchString];
min = [min decimalNumberBySubtracting: delta];
NSDecimalNumber *max = [NSDecimalNumber decimalNumberWithString: searchString];
max = [max decimalNumberByAdding: delta];
filterPredicate = [NSPredicate predicateWithFormat: @"myNSDecimalValue BETWEEN %@", @[min, max]];
到此为止,它的工作正常,谓词看起来像:
myNSDecimalValue BETWEEN {-3, 7}
如果我尝试使用
过滤我的数据NSArray *filteredArray = [myArray filteredArrayUsingPredicate: filterPredicate];
我崩溃了:
'NSInvalidArgumentException', reason: '-[_UITextKitTextPosition compare:]: unrecognized selector sent to instance 0x7ffa32cb3e60'
*** First throw call stack:
(
0 CoreFoundation 0x0000000102f1ef45 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010255bdeb objc_exception_throw + 48
2 CoreFoundation 0x0000000102f2756d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x0000000102e74eea ___forwarding___ + 970
4 CoreFoundation 0x0000000102e74a98 _CF_forwarding_prep_0 + 120
5 Foundation 0x000000010217d711 -[NSDecimalNumber compare:] + 118
6 Foundation 0x0000000102241dcf -[NSBetweenPredicateOperator performPrimitiveOperationUsingObject:andObject:] + 235
7 Foundation 0x000000010214c7c9 -[NSPredicateOperator performOperationUsingObject:andObject:] + 286
8 Foundation 0x000000010214beec -[NSComparisonPredicate evaluateWithObject:substitutionVariables:] + 313
9 Foundation 0x000000010214bd44 _filterObjectsUsingPredicate + 398
10 Foundation 0x000000010214bb34 -[NSArray(NSPredicateSupport) filteredArrayUsingPredicate:] + 279
显然,使用NSDecimalNumber
的谓词存在问题。崩溃的行[_UITextKitTextPosition compare:]
也很奇怪。在我使用浮点数之前它运行正常,但我正在转换为NSDecimalNumber
以便在计算中获得更好的精度。
是否可以将NSDecimalNumber
与NSPredicate
一起使用?
更新
问题解决了。当我从float
切换到NSDecimalNumber
时,我无法将该属性从assign
更改为strong
。