ios - swift 中的 NSPredicate 'IN' 运算符格式化问题。无法解析格式字符串

标签 ios swift core-data nspredicate

我在尝试创建如下所示的 NSPredicate 对象时遇到异常:

let searchPredicate = NSPredicate(format: "addonCategoryId IN \(idsArray)")

其中 idsArray 是由 Int 对象组成的 swift 数组。此行本身引发异常。 控制台说:

2015-10-08 08:21:42.878 bkApp[2569:510538] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "addonCategoryId IN [5404, 5406]"'

可能是什么问题?谢谢参观。 :)

最佳答案

NSPredicate 仍然期待 Objective-C 风格的格式字符串。您使用了字符串插值。

let searchPredicate = NSPredicate(format: "addonCategoryId IN \(idsArray)")
// result: addonCategoryId IN [5404, 5406]

试试这个:

let searchPredicate = NSPredicate(format: "addonCategoryId IN %@", idsArray)
// result: addonCategoryId IN {5404, 5406}

区别在于微妙的 []{} 但如果 NSPredicate 不喜欢它,它就不喜欢它。

关于ios - swift 中的 NSPredicate 'IN' 运算符格式化问题。无法解析格式字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33005876/

相关文章:

ios - "' CGFloat ' is not convertible to ' UInt8 '"- CGFloat 包装问题

ios - 使用核心数据从 TableView 打开编辑 View Controller

ios - 非法尝试在不同上下文中的对象之间建立关系 'checklists'

ios - swift 。导航 Controller 。有条件地更改 subview Controller 的顺序

ios - 禁用多个按钮的简单方法?

swift - NavigationItem TitleView 约束不起作用

ios - 我可以在 coredata 中存储多少条记录

ios - 如果不满足某些条件,如何防止保存输入

ios - Apple 的 ResearchKit 中的额外本地化是否导致 iOS App Store 错误地认为我们的应用程序已针对许多其他语言进行了本地化?

ios - 保留UITouch : allowed or not?