Comments (6)
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.
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.
Ah, this could be 2 things.
- 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
- 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.
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.
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.
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)
- Nested Generics HOT 1
- Swift 3 and Alamofire 4 HOT 3
- get Array with key HOT 3
- Migration help needed HOT 9
- Could not create an instance for type "Forecast" HOT 10
- [Question] What happens if the response is not of expected type? HOT 5
- Swift 2.3 and Alamofire 3.5 support? HOT 3
- 1.6 version uses EVReflection swift 3 version HOT 2
- .description outputs a boolean as a number (0 or 1) HOT 3
- Mapping list of objects? HOT 2
- Use legacy swift error with swift 3 HOT 4
- Incorrect arguments label in call HOT 12
- Is not key value coding-compliant for the key __response_statusCode? HOT 10
- Error parsing array swift 3 HOT 2
- Array mapping return empty array HOT 13
- Help with usage HOT 2
- Build error when including as CocoaPod HOT 4
- Swift Compiler Error for Xcode 8.3 HOT 7
- Problems after updating the pod and to Swift4 HOT 1
- Compiling for iOS 8.0, but module 'Alamofire' has a minimum deployment target of iOS 10.0
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from alamofirejsontoobjects.