vendredi 5 mai 2017

Mirroring Not Picking Up Base Class on Swift 3

I am building a function that will take an instance of a class object and convert it to an XML request to be sent to a web service. To accomplish this, I am using mirroring to iterate through the key/value pairs in the class. In my testing, I see it is working great, with one major problem, none of the inherited class parameters are coming across. For example, in the code below, the loop is executed three times for "descriptionText, modelNumber and serialNumber, name and uuid are never collected. Is there a way for me to use mirroring and pick up all the parameters of the base class, as well as the widget? Also, if there is a better way to do this, I am all ears.

import UIKit

var str = "Hello, playground"

class baseObject : NSObject{
   var name = String()
   var uuid = String()
}

class Widget : baseObject{
    var descriptionText = String()
    var modelNumber = String()
    var serialNumber = String()
}

var widget1 = Widget()

widget1.name = "Generic Widget"
widget1.uuid = "A guid"
widget1.descriptionText = "Class A Extra Larget Widget"
widget1.modelNumber = "1234"
widget1.serialNumber = "4321"

let widgetMirror = Mirror(reflecting: widget1)

for (_, attr) in widgetMirror.children.enumerated(){
    print(attr.label as String!)
}





Aucun commentaire:

Enregistrer un commentaire