Firstly there is a base class be called BaseModel ,below is the code:
class BaseModel : NSObject
{
var code:Int?
var message:Any?
public func setDictionaryToAttributes(dictionary_is dictionary:Dictionary<String,Any>?)->Bool
{
guard let dic:Dictionary<String,Any> = dictionary else {return false}
for (key,value) in dic {
let someType = type(of: value)
debugPrint("\(String(describing: someType.self))")
debugPrint("\(String(describing: someType))")
if someType is Array<Any>.Type { //i can't get TestListItemModel string
debugPrint("\(String(describing: someType))")
}
}
self.setValuesForKeys(dic)
return true;
}
}//class BaseModel end
and there is another class inherited from BaseModel
class TestListModel: BaseModel {
var tatalink:Any?
var items:Array<TestListItemModel>?
func setValuesFrom(jsonData j:JSON) { //j is a swifyJson object
guard var dic = j.dictionaryObject else {return}
if self.setDictionaryToAttributes(dictionary_is: dic)==false {
return
}
}
}
there is a class TestListItemModel for submodel in TestListModel
class TestListItemModel:BaseModel {
var imgurl: Any?
var title: Any?
}
Question is: I want to automatically parse all attribute values in the BaseModel class from json data. in func analysetDictionaryToAttributes: I can find which one is Array, but I don't know how to get this type and Continue to call it's analysetDictionaryToAttributes func.
Why do you expect to get TestListItemModel
in your BaseModel
class function. First of all I cannot see any structure having TestListItemModel
. Because I can see you are just passing json dictionary object which is unaware of your class 'TestListItemModel' in this line
self.setDictionaryToAttributes(dictionary_is: dic)
Then why do you expect that parent class will know about its child class here is TestListItemModel
and TestListModel
.
So in this function
public func setDictionaryToAttributes(dictionary_is dictionary:Dictionary<String,Any>?)->Bool
you will always get Dictionary of String as a key and Value as Any type. And if you are expecting String then you can always check it this way
if value = value as String
{
print(value)
}