0

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.

1 Answers1

0

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)
}
Krishna Kumar
  • 1,652
  • 9
  • 17
  • thank you for your answer, there are multiple a pair of KEY and VALUE in the dictionary, like this:("code":"1")("message":"xxx")("tatalink":"xxx")("items":"[xxx,xxx,xxx]") and i hope Only one function can parse all attributes in BaseModel – xuanwenchao Jun 23 '17 at 07:49
  • But baseModel will never know that if there are further childModels.. And if you want to parse it in childModels.. You can parse it in generic way i.e [String : Any] .. – Krishna Kumar Jun 23 '17 at 08:16
  • YES, My difficulty is that always tell me type is Array in BaseModel, but I've always thought that we can know it in run time,link dynamic binding principle. – xuanwenchao Jun 23 '17 at 08:19
  • You can serialise your json by using options like mutableContainers.. it will parse it out for container structure.. – Krishna Kumar Jun 23 '17 at 08:21
  • what would be the best way to do that? – xuanwenchao Jun 23 '17 at 08:25
  • If you want to store values in different models. Then I would say create those models without subclassing from Base Model and call it from BaseModel.. and then you can subclass it if you want to override over this default behaviour or add something to it.. – Krishna Kumar Jun 23 '17 at 08:32
  • => Then I would say create those models without subclassing from Base Model and call it from BaseModel. it's means without inherited from BaseMode? I don't quite understand. Does it make any difference? – xuanwenchao Jun 23 '17 at 09:27
  • When you will not inherit then you can use it in BaseModel to create a collection of different models. And then if you want to leverage that function or need to add or override you can inherit BaseModel.. – Krishna Kumar Jun 23 '17 at 10:00
  • Consider this VehicleModel containing different models like wheel, engine etc and then you can inherit VehicleModel to get your CarModel, BusModel etc.. – Krishna Kumar Jun 23 '17 at 10:02
  • I just want to know,array , this CustomModel In any case, can't I get it on rumtime? – xuanwenchao Jun 23 '17 at 10:32
  • just like ivar_getTypeEncoding method, but it is now work in swift, return value is empty. – xuanwenchao Jun 23 '17 at 11:20
  • Ofcourse you can but you will have to hold CustomModel object in array.. But in your example BaseModel does not know about CustomModel.. but CustomModel knows about BaseModel.. – Krishna Kumar Jun 23 '17 at 15:14