14

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 ?

Matthieu Riegler
  • 31,918
  • 20
  • 95
  • 134

1 Answers1

42

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 !

Matthieu Riegler
  • 31,918
  • 20
  • 95
  • 134
  • 4
    Swift classes are namespaced and using the `@objc` makes the class available without any namespacing. The class should be available in objc if you identify it by the Swift module name (usually your app's name) and the class: `MyCoreDataApp.LocationArrayTransformer`. – sbooth Sep 15 '15 at 23:08
  • 1
    Works like a charm ! Thanks for the heads-up ! – Matthieu Riegler Sep 15 '15 at 23:30
  • I better not asked how much time you've spent to find this solution, but I can tell you that you saved me a ton. thx – mnl Sep 29 '17 at 09:04
  • Exactly `@objc(LocationArrayTransformer)` does the trick. Thanks – Alexey Ishkov Jul 04 '19 at 05:09
  • Thanks from 2022 ) Answer from the first google link, rare beast these days. ) – Wilhelm Lake Jan 14 '22 at 15:27
  • Amazing! Thank you for your time on figuring out the answer and for sharing with the rest of us to benefit from your hard work. This had the working answer that I needed. – David_2877 Mar 17 '23 at 08:51