Search code examples
iosswiftcore-data

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 ?


Solution

  • 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 !