Validate "Any" with validateValue: forKey in CoreData

187 views Asked by At

I'm writing to CoreData with key-values:

entity.setValue(value: <Any?>, forKey: <String>)

Before writing to CoreData I store the value and the field name in vars:

var value: Any?
var field: String

I would like to do a simple validation before I attempt to write to CoreData. The function validateValue(_:forKey:) seems to satisfy my needs but I don't understand how to use it:

try entity.validateValue(_ value: AutoreleasingUnsafeMutablePointer<AnyObject?>, forKey: String)

In my case it would be like:

    do {
        try entity.validateValue(value, forKey: field)
    } catch {
        let validationError = error as NSError
        print(validationError)
    }

But how do I pass

var value: Any

to

AutoreleasingUnsafeMutablePointer<AnyObject?>

Is it even possible to use the validateValue function in this context?

Thanks!

1

There are 1 answers

1
vrwim On

You should be able to do let pointer = AutoreleasingUnsafeMutablePointer<AnyObject?>(&value).

You should read the docs of validateValue here to ensure you are using it correctly (e.g. implementing validate<Key>:error:)