dimanche 4 octobre 2015

Reflection in swift 2

I have a class User:

import UIKit
import ObjectMapper


class User: NSObject, CustomStringConvertible, Mappable {

    var FirstName: NSString! ;
    var LastName: NSString! ;


    required init?(_ map: Map){

    }


    func mapping(map: Map) {

        FirstName <- map["FirstName"]
        LastName <- map["LastName"]

    }


    override var description:String {
        var s:String=""

  //USE REFLECTION TO GET NAME AND VALUE OF DATA MEMBERS      
        for var index=1; index<reflect(self).count; ++index {
            s += (reflect(self)[index].0 + ": "+reflect(self)[index].1.summary+"\t")
        }

        return s
    }
}

In swift 1.2, I was using reflect() method to get array of all the data members with their names and values.

Now, after I have updated to swift 2, I am getting the following error:

'reflect' is unavailable: call the 'Mirror(reflecting:)' initializer

With some trials, I was able to get the count of data members by this: Int(Mirror(reflecting: self).children.count), but still, I am unable to get the member name and its value.

I have looked into the following resources: 1. http://ift.tt/1K6V8Cj 2. http://ift.tt/1Fu7Kvm





Aucun commentaire:

Enregistrer un commentaire