【问题标题】:Very strange NSPredicate behavior when logging记录时非常奇怪的 NSPredicate 行为
【发布时间】:2012-02-04 19:53:25
【问题描述】:

我正在尝试创建一个谓词来对具有日期属性的对象数组进行排序。但是,当我尝试 NSLog 谓词时,会引发异常并且出现奇怪的错误。

代码如下:

NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy"];
NSDate * minDate = [dateFormatter dateFromString:@"2011"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"date >= %@ ", minDate];
NSLog(predicate);

这是错误:

2012-02-04 12:45:58.400 MyApp[42746:17303] -[NSComparisonPredicate length]: unrecognized selector sent to instance 0x9359000

那么,为什么会发生这种情况,我该如何解决?

【问题讨论】:

  • 您尝试应用谓词的确切属性类型是什么?
  • NSDate。但是,我什至没有到达我实际使用谓词的地方。
  • 我不确定将 NSDates 与这样的谓词进行比较是否有效。是的,你不能像那样使用 NSLog()。
  • 我认为,谓词本身是可以的。

标签: objective-c ios cocoa-touch nspredicate


【解决方案1】:

试试:NSLog(@"%@", predicate)

NSLog 需要一个 NSString,它作为格式字符串工作。格式字符串中的格式说明符的数量将指定,需要遵循多少个参数。

来自doc:

NSLog
将错误消息记录到 Apple 系统日志工具。

void NSLog (
   NSString *format,
   ...
);  

讨论
只需调用 NSLogv,传递给它一个可变数量的 论据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-13
    • 2011-03-01
    • 2014-03-21
    • 2020-12-02
    • 1970-01-01
    • 1970-01-01
    • 2018-04-20
    相关资源
    最近更新 更多