NSPredicate 和 NSString 问题
我有以下谓词,可以很好地处理 555 和 623(3 位数字)等数字。
问题是,我有 005 和 009 等数字(1 位数字,前面有两个 0)。
NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"ANY code BETWEEN %@",
[NSArray arrayWithObjects:
[NSExpression expressionForConstantValue: [NSNumber numberWithFloat: [self.predicateFilter floatValue]]],
[NSExpression expressionForConstantValue: [NSNumber numberWithFloat: [self.predicateFilter floatValue] + .99]],
nil]];
大多数时候,predicateFilter 是一个带有数字的字符串,如 @"124"
。但是,当我运行像 035 这样的数字(注意开头的 0)时,我得到以下 NSLog:
2011-08-13 18:32:23.989 Codes[2007:707] code 004
2011-08-13 18:32:24.018 Codes[2007:707] ANY code BETWEEN {4, 4.99}
2011-08-13 18:34:23.846 Codes[2007:707] code 083
2011-08-13 18:34:23.864 Codes[2007:707] ANY code BETWEEN {83, 83.99}
因此,由于 00X
,它找不到这些数字。
有没有办法可以执行 if/else 或捕获所有以 0
或 00
开头的数字,以便我可以相应地编辑谓词?
编辑:
我想我只需要创建一个谓词,将 code
的前 3 位数字与 NSString 匹配,即 005。然后这会给我其余的结果,即 005.5、005.6、 ETC。
I have the following predicate that works fine with numbers such as 555 and 623 (3 digit numbers)
The problem is, I have numbers such as 005 and 009 etc (1 digit with two 0 before).
NSPredicate *filterPredicate = [NSPredicate predicateWithFormat:@"ANY code BETWEEN %@",
[NSArray arrayWithObjects:
[NSExpression expressionForConstantValue: [NSNumber numberWithFloat: [self.predicateFilter floatValue]]],
[NSExpression expressionForConstantValue: [NSNumber numberWithFloat: [self.predicateFilter floatValue] + .99]],
nil]];
Most of the time, predicateFilter will be a string with number like @"124"
. But when I run a number like 035 (notice the 0 at beggining), I get the following NSLog:
2011-08-13 18:32:23.989 Codes[2007:707] code 004
2011-08-13 18:32:24.018 Codes[2007:707] ANY code BETWEEN {4, 4.99}
2011-08-13 18:34:23.846 Codes[2007:707] code 083
2011-08-13 18:34:23.864 Codes[2007:707] ANY code BETWEEN {83, 83.99}
So it is not finding those numbers because of the 00X
.
Is there a way I can do an if/else or something that catches all numbers that have a 0
or 00
to begin with, so I can edit the predicate accordingly?
EDIT:
I think I just need to make a predicate that matches the first 3 digits of code
to an NSString, which as 005. Then that would give me the rest of the results that are 005.5, 005.6, etc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于 code 是一个字符串,您可以改为进行 MATCH 比较,或者这是核心数据。如果它是核心数据,您可以添加一个属性以将code返回为整数codeInteger,也许使用瞬态属性。
Since code is a string can you do a MATCH comparison instead, or is this core data. If it is core data can you add a property to return code as an Integer codeInteger perhaps using transient attribute.