How to call validateValue method

I am trying to create a general implementation of NSCoding and have a problem with decoding when the type of the object has changed on average due to a newer version of the application.

I have a problem calling the validateValue method from Swift. Function Signature:

func validateValue(_ ioValue: AutoreleasingUnsafeMutablePointer<AnyObject?>, forKey key: String, error outError: NSErrorPointer) -> Bool 

For reference, Apple's documentation can be found here: NSCoding validateValue

The code I want to create is:

 public class func decodeObjectWithCoder(theObject:NSObject, aDecoder: NSCoder) { for (key, value) in toDictionary(theObject) { if aDecoder.containsValueForKey(key) { let newValue: AnyObject? = aDecoder.decodeObjectForKey(key) NSLog("set key \(key) with value \(newValue)") var error:NSError? var y:Bool = theObject.validateValue(newValue, forKey: key, error: &error) if y { theObject.setValue(newValue, forKey: key) } } } } 

I cannot correctly call .validateValue. I keep getting compilation errors. What should I call it? The toDictionary function can be found at: EVReflection

Update : I just found out that this code compiles:

  var ioValue: AutoreleasingUnsafeMutablePointer<AnyObject?> = nil var y:Bool = theObject.validateValue(ioValue, forKey: key, error: nil) 

Does this mean a value that is of type AnyObject? cannot be sent to AutoreleasingUnsafeMutablePointer

+5
source share
1 answer

The newValue field should be passed as a pointer, adding in front of it. In addition, you should use var instead of let. Thus, the final code will be:

 public class func decodeObjectWithCoder(theObject:NSObject, aDecoder: NSCoder) { for (key, value) in toDictionary(theObject) { if aDecoder.containsValueForKey(key) { var newValue: AnyObject? = aDecoder.decodeObjectForKey(key) if theObject.validateValue(&newValue, forKey: key, error: nil) { theObject.setValue(newValue, forKey: key) } } } } 
+1
source

Source: https://habr.com/ru/post/1212061/


All Articles