SWIFT - How to get an array type?

I am writing a general method that takes a dictionary and a given type in the parameters for assembling an object.

For example, if you make a SOAP request to receive a movie and postpone the answer in the dictionary, you can do:

var movie : Movie = myGenericMethod(dic : Dictionary, objectToIntrospect : Movie()) as Movie

Works with:

  • Simple object
  • Complex object

But I have a problem if you have an array of objects. So, imagine that your movie object contains an array of actors ...

With reflection, I get all types of class attributes. In doing so, I create an array of any object that contains my types. For example, an object contained in another object (Actor in Movie):

//All type of attributes of my movie object, at index [i] i have my "Actor" object
var anyType : Any = typesOfProperties[i]

//I cast it in object
var objectType : NSObject = anyType as NSObject

//Dont worry about this method, it just for get the dictionary
var otherDico : NSDictionary = ConverterUtilities.extractDictionaryFromOtherDictionary(dict, dicoName: property, soapAction: soapAction) as NSDictionary

//I build the Actor object with the data of the dictionary. objectType.self give the Actor type
var propertyObject: NSObject = self.buildAnyObjectWithDictionary(otherDico, soapAction: "", objectToIntrospect:objectType.self) as NSObject

//I set the property Actor in my Movie object (myObjectToReturn)... The "property" parameter is the key 
ConverterUtilities.setPropertyValue(property, value: propertyObject, objectToReturn : myObjectToReturn, isObject : true)

It works fine ... If I have only one actor in my movie object, "propertyObject" will be of type Actor, and this will call objectType - an Actor object.

, , Array, objectType "Swift._NSSwiftArrayImpl" anyType return "([myproject.Actor])". , , . , Actor!

, :

var objToAdd: NSObject = self.buildAnyObjectWithDictionary(newDic, soapAction: "", objectToIntrospect: Actor()) as NSObject

arraySpecific.append(objToAdd)

, , . , ! :

var objToAdd: NSObject = self.buildAnyObjectWithDictionary(newDic, soapAction: "", objectToIntrospect: anObjectWithActorType) as NSObject

arraySpecific.append(objToAdd)

( - objectToIntrospect)

, Any (: ([myproject.Actor]) ?

! !

PS: , , :)

+4
1

, , , : - .

getArrayType.

, , getArrayType.

:

override func getArrayType(anyType : Any) -> String {
    var className = " "
    if(anyType as? NSObject == rows){
        className = Properties.PROJECT_NAME + ".SimpleRow"
    }
    return className
}

(TObject - ):

var className = (objectToIntrospect as TObject).getArrayType(anyType)
var obj: AnyClass! = NSClassFromString(className)

, . , , !

+1

All Articles