Giter Club home page Giter Club logo

Comments (6)

evermeer avatar evermeer commented on June 16, 2024

indeed you have to use the setValue forUndefinedKey.
here is a unit test that will show you how to use generics:
https://github.com/evermeer/EVReflection/blob/68edace301602dd43181075c604a1f987185be3f/EVReflection/EVReflectionTests/WorkaroundSwiftGenericsTests.swift#L16-46
And to make that work, you have to add the EVGenericsKVC protocol after your class definition

from alamofirejsontoobjects.

sagits avatar sagits commented on June 16, 2024

Thank you, it worked, but the [T] was not being detected as a [ProductModel]. A have been able to cast a single T to a Object, but not [T] to [Objects], example:

           //var list = response!.records as! [ProductModel]! this wont work, as ProductModel is not a U

           var list = response!.records as [U]!

            for product in list {
                var products : [ProductModel] = [ProductModel]();

                var p = product as! ProductModel
                products.append(p)
                println(product is ProductModel)
                println("id: " + p.name!);
            }

Is there a way to cast the entire list (sorry for asking, but i really cant found anything about it)? By the way, i'm trying to archieve this pattern:

ViewController:

    var c : CallListListener<ProductModel> = CallListListener<ProductModel>()
    GenericDao<ProductModel>().getAll(c);

GenericDao

  Alamofire.request(method, serverUrl + URL, headers: getHeaders())
      .responseObject {
          (response: JsonMessageList<U>?, error: NSError?) in

Thanks in advance.

from alamofirejsontoobjects.

evermeer avatar evermeer commented on June 16, 2024

Ah, this could be 2 things.

  1. a generic array is of type Any and not AnyObject or NSObject. Then you should implement something like this: https://github.com/evermeer/EVReflection/blob/bb630dfb7759f3269ad872ba9e01b133d7b8e67c/EVReflection/EVReflectionTests/WorkaroundsTests.swift#L83-97
  2. in the setValue forUndefinedKey you will receive an array of dictionaries. So instead of assigning and casting it directly, you have to map it to your objects like this:
    array = (value as! NSArray).map({T(dictionary:$0)})

At the moment I can't create a sample for you. I will have a look at it tomorrow.

from alamofirejsontoobjects.

evermeer avatar evermeer commented on June 16, 2024

I was wrong. It should just work. I created a new unit test that shows this. Here it is:
https://github.com/evermeer/EVReflection/blob/c0de1d56f2589df7c0650a33ddb06b539a116d2b/EVReflection/EVReflectionTests/WorkaroundSwiftGenericsTests.swift#L25-35

In your json, do you have a root object with the property records which will contain an array of your product model records? So something like this: {"records":[{"property":"value"},{"property":"value2"}]}
Or is the root of the json already an array? like this: [{"property":"value"},{"property":"value2"}]
If so, then you need to use .responseArray instead of .responseObject

from alamofirejsontoobjects.

sagits avatar sagits commented on June 16, 2024

Thank you for your time man, now its working, My json was an object with an array inside. But i'm ashamed too say that my error was not typing records:

 var records = [T]()

Should be:

    var records : [T] = [T]()

My mistake, sorry. And just for helping others: Appcode will not convert your T objects to the actual model type (it will not autocomplete, but if you type list[0].someproperty it will work).

from alamofirejsontoobjects.

evermeer avatar evermeer commented on June 16, 2024

Great that I could be of help. And now there are more unit tests that show how it should work. And we know that it does work. So no problem!

from alamofirejsontoobjects.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.