简体   繁体   English

可转换的 CoreData NSPredicate 导致 EXC_BAD_ACCESS

[英]CoreData NSPredicate on a Transformable causes EXC_BAD_ACCESS

What is the correct way to search for existence of an array element in a Transformable in Core Data using NSPredicate ?使用NSPredicate在 Core Data 中的Transformable中搜索数组元素是否存在的正确方法是什么?

I have an Entity with an Attribute operationalDays with a Type of Transformable which is set given a Custom Class [Int] .我有一个具有可Transformable TypeAttribute operationDays 的Entity ,该Entity是给定Custom Class [Int] 在此处输入图片说明 I use it to store, well Arrays of Ints.我用它来存储整数数组。 It works to store but I get EXC_BAD_ACCESS when trying to fetch them using an NSPredicate它的工作原理到店,但我得到EXC_BAD_ACCESS时尝试使用NSPredicate获取它们

I Know我知道

operationalDays is {1,2,3,4}操作天数是 {1,2,3,4}

I am trying but they all fail with error.我正在尝试,但它们都因错误而失败。

//day is an Int (1) in this case

NSPredicate(format: "%d IN operationalDays",day) 
NSPredicate(format: "%i IN operationalDays",day) 
NSPredicate(format: "%@ IN operationalDays",day) 
NSPredicate(format: "operationalDays CONTAINS %d",day) 
NSPredicate(format: "operationalDays CONTAINS %i",day)
NSPredicate(format: "operationalDays CONTAINS %@",day)

when I inspect the predicate after crash I get当我在崩溃后检查谓词时,我得到

predicate: (1 IN operationalDays);谓词:(1 IN operationDays);

or要么

predicate: (operationalDays CONTAINS 1)谓词:(operationalDays CONTAINS 1)

The transformable attributes are stored as binary data in the Sqlite DB.可转换属性作为二进制数据存储在 Sqlite DB 中。 You can't run a query on that data unless your predicate has binary data as well.除非您的谓词也有二进制数据,否则您无法对该数据运行查询。 Transformable attributes abide by NSCoding protocol, hence, Core Data knows how to serialize and deserialize the attribute.可转换属性遵循 NSCoding 协议,因此 Core Data 知道如何序列化和反序列化属性。

PS To see the error that Sqlite throws, try adding PS要查看Sqlite抛出的错误,尝试添加

-com.apple.CoreData.SQLDebug 1

to the "Arguments Passed on Launch" in your Xcode scheme.到您的 Xcode 方案中的“启动时传递的参数”。

在此处输入图片说明

你应该使用这种风格的谓词

let predicate = NSPredicate(format: "%@ IN days", argumentArray: [valueOfArgumentHere])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM