evermeer / AlamofireJsonToObjects

An Alamofire extension which converts JSON response data into swift objects using EVReflection
Other
161 stars 28 forks source link

Nested Generics #25

Closed Twinsens closed 8 years ago

Twinsens commented 8 years ago

The issue may be dublicated with EVReflection issues page. Bu I dont know where to I write. I just need help. :(

Hello

Is that possible nested generics in EVReflection? I was tested code that the following but it not work. Could you help me about the issue?

class ResponseModel<T:BaseModel> : EVObject,EVGenericsKVC {    

      required init() {
          super.init();
        }

    var success : String?;
    var reason : String?
    var content : [T]?;

    internal override func setValue(value: AnyObject!, forUndefinedKey key: String) {
        if(key == "content"){
            content = value as? [T];
        }
    }

}

class PagerModel<T:BaseModel>: BaseModel,EVGenericsKVC{    
    required init() {
        super.init();
    }

    var total:String?;
    var per_page:String?;
    var current_page:String?;
    var last_page:String?;
    var next_page_url:String?;
    var prev_page_url:String?;
    var from:String?;
    var to:String?;
    var data:[T]?;

       internal  override func setValue(value: AnyObject!, forUndefinedKey key: String) {
        if(key == "data"){
        data = value  as? [T];
        }
    }

class BaseWebServices<T:BaseModel> : NSObject  {

    let BASE_URL : String! = "http://localhost/IOS/public/api/v1/";

    var listener : ResponseListener?;

    func executeService(serviceUrl : String, method : Alamofire.Method,parameters: [String: AnyObject]){

        let URL : String = "\(self.BASE_URL)\(serviceUrl)";
        Alamofire.request(method, URL, parameters:parameters)
            .responseObject { (response: Result<ResponseModel<T>, NSError>) in

                switch response{
                case .Success( _)  :
                    if let result : ResponseModel<T> = response.value {
                        self.listener?.onResponseSuccess(result);
                    }
                    break;
                case .Failure(let error) :
                    self.listener?.onResponseFail(error)
                    break;
                }
        }

    }

class NewsHeaderService: BaseWebServices<PagerModel<NewsHeader>>,ResponseListener {

    override  init() {
        super.init();
        super.listener = self;

        executeService("getNewsHeadersByPrjId/1", method: .POST, parameters: ["token":Common.TOKEN_KEY])
    }

    func onResponseSuccess<T : BaseModel where T : protocol<ObjectProtocol>>(object: ResponseModel<T>) {

    }

    func onResponseFail(failMessage: NSError) {

    }
}

class NewsHeader : BaseModel{   

    var id : String?;
    var Description: String?;
    var newsTypeId:String?;
    var newsOrder : String?;
    var isCommentActive : String?;
    var placeTypeId : String?;
    var link : String?;
    var title : String?; 
    var pubDate : String?;
    var imageUrl : String?;
    var imageThumUrl : String?;
    var appId : String?;
    var projectId : String?;
    var isValid : String?;
    var created_at : String?;
    var updated_at : String?;
}

When I try to execute

var service : NewsHeaderService = NewsHeaderService();

There was an error,

ERROR: Could not create an instance for type PagerModel dict:{ "current_page" = 1; data = ( { appId = 1; "created_at" = "2016-06-29 12:15:23"; description = 33333; id = 1; imageThumUrl = ""; imageUrl = "uploads/74d41d1a8b39541ce2e7b6fc1f844ef0_04_IP-MAN-3_Courtesy-of-Well-Go-USA_0-752x440.jpg"; isCommentActive = 1; isValid = 1; link = 11111; newsOrder = 0; newsTypeId = 2; placeTypeId = 1; projectId = 1; pubDate = ""; title = 22222; "updated_at" = "2016-06-29 12:15:23"; } ); from = 1; "last_page" = 1; "next_page_url" = ""; "per_page" = 15; "prev_page_url" = ""; to = 1; total = 1; }

If I change class PagerModel<T:BaseModel> toclass PagerModel and remove "data" property it works fine but I need the data :) Can anybody help me about my issue? Thanks for your time.

Edit: Here is parsed JSON data

{"success":true,"reason":"OK","content":[{"total":1,"per_page":15,"current_page":1,"last_page":1,"next_page_url":null,"prev_page_url":null,"from":1,"to":1,"data":[{"id":1,"newsTypeId":"2","newsOrder":"0","isCommentActive":"1","placeTypeId":"1","link":"11111","title":"22222","description":"33333","pubDate":"","imageUrl":"uploads\/74d41d1a8b39541ce2e7b6fc1f844ef0_04_IP-MAN-3_Courtesy-of-Well-Go-USA_0-752x440.jpg","imageThumUrl":"","appId":"1","projectId":"1","isValid":"1","created_at":"2016-06-29 12:15:23","updated_at":"2016-06-29 12:15:23"}]}]}

evermeer commented 8 years ago

This is indeed a EVReflection issue. I will pick this up in the issue that you reported there

https://github.com/evermeer/EVReflection/issues/114