简体   繁体   中英

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 ?

I have an Entity with an Attribute operationalDays with a Type of Transformable which is set given a 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

I Know

operationalDays is {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);

or

predicate: (operationalDays CONTAINS 1)

The transformable attributes are stored as binary data in the 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.

PS To see the error that Sqlite throws, try adding

-com.apple.CoreData.SQLDebug 1

to the "Arguments Passed on Launch" in your Xcode scheme.

在此处输入图片说明

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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