Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing NSValueTransformer for Transformable attributes in Swift

I'm trying to implement a NSValue Transformer, which should help me to save a Double Array into Core Data using a Transformable attribute.

So I tried to implement the transformedValueClass. But NSArray.class() is crossed out. Unfortunately I didn't find a reason for this.

My method looks like this:

class PacePerK:NSValueTransformer{
    class func transformedValueClass() -> AnyClass!
    {
        return NSArray.class()
    }
}

I get the following compiler errors: Expected member name following '.' Expected identifier in class declaration

Unfortunately they don't really help me.

Why is the NSArray.class crossed out? How can I return the class of NSArray without causing a compiler error?

like image 896
rohx Avatar asked Jun 15 '14 13:06

rohx


People also ask

What is transformable property in Swift language?

With the support of all of these properties Swift language is also providing you Transformable property which is very Interestingly Important. Because of this property you can put any type of data into the Core Data without extra overhead. In this tutorial I am going to show you how to use both of these properties...

Which data type must be considered in Swift core data?

Out of all of these properties Binary Data is must to be considered. With the support of all of these properties Swift language is also providing you Transformable property which is very Interestingly Important. Because of this property you can put any type of data into the Core Data without extra overhead.

What are Interface Builder attributes in Swift?

Interface Builder attributes are declaration attributes used by Interface Builder to synchronize with Xcode. Swift provides the following Interface Builder attributes: IBAction, IBSegueAction, IBOutlet, IBDesignable, and IBInspectable.

How to create a nsmanagedobject in Swift 5?

5. Keep the entity "UseOfDataTypes" selected we are going to create a NSObject Class. Click on the editor && then click on the Create NSmanagedObject Subclass option && follow the instructions until the class with the same name as entity will be created.


1 Answers

It appears the correct why to reference class objects in Swift is with:

NSArray.self

Also, in your example, you’ll need to mark your transformedValueClass function with “override”. So:

class PacePerK:NSValueTransformer{
    override class func transformedValueClass() -> AnyClass!
    {
        return NSArray.self
    }
}
like image 168
joshstaiger Avatar answered Oct 17 '22 01:10

joshstaiger