Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data : No NSValueTransformer with class name XXX was found for attribute YYYY on entity ZZZZ

I have my CoreData model set in a xcdatamodel file.

My attribute YYYY has a type transformable and I set the tranformer name in the Data model inspector.

I my case I was storing a [CLLocation] in my model.

class LocationArrayTransformer : NSValueTransformer {


    override func transformedValue(value: AnyObject?) -> AnyObject? {

        let locations = value as! [CLLocation]

        return NSKeyedArchiver.archivedDataWithRootObject(locations)
    }

    override func reverseTransformedValue(value: AnyObject?) -> AnyObject? {

        let data = value as! NSData

        return NSKeyedUnarchiver.unarchiveObjectWithData(data)
    }


}

That's my value transformer.

But somehow I'm still getting the warning in the console : No NSValueTransformer with class name XXX was found for attribute YYYY on entity ZZZZ

Any Idea why ?

like image 941
Matthieu Riegler Avatar asked Sep 15 '15 23:09

Matthieu Riegler


1 Answers

I spent way to much time on this to not share the solution I found :

I had to make the NSValueTransformer subclass available to Objc.

@objc(LocationArrayTransformer)
class LocationArrayTransformer : NSValueTransformer {
 ....
}

Simple as that.


As @Sbooth points out, Swift classes are namespaced. Using @objc makes the class available without namespacing. So setting the transformer name as MyApp.Mytransformer works well too !

like image 197
Matthieu Riegler Avatar answered Oct 17 '22 22:10

Matthieu Riegler