2015-01-27 15 views
5

Sto provando a creare un'implementazione NSCoding generica e ho un problema con la decodifica quando il tipo di oggetto è stato modificato nel frattempo a causa di una versione più recente dell'app.Come chiamare il metodo validateValue

Ho un problema con la chiamata del metodo validateValue da Swift. La firma funzione è:

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

A titolo di riferimento, la documentazione di Apple può essere trovato qui: NSCoding validateValue

Il codice che voglio creare è:

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) 
       } 
      } 
     } 
    } 

non posso ottenere la chiamata a .validateValue è corretta. Continuo a ricevere errori di compilazione. Come dovrei chiamarlo. La funzione ToDictionary sono disponibili all'indirizzo: EVReflection

Aggiornamento: Ho appena scoperto che questo codice fa compilare:

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

Ciò significa che il valore che è di tipo ANYOBJECT? non può essere assegnato a AutoreleasingUnsafeMutablePointer

risposta

1

Il campo newValue ha dovuto essere passato come un puntatore aggiungendo & di fronte ad esso. Oltre a questo devi usare var invece di let. Quindi il codice finale sarà:

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) 
      } 
     } 
    } 
} 
Problemi correlati