-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New Objective C syntax (lightweight generics) #376
Comments
W/ Xcode 7 you have to forward-declare protocol name explicitly to suppress compiler error |
Hope @pjebs opinion can be accepted. |
Find the answer : http://stackoverflow.com/questions/30866197/how-can-i-find-out-the-objective-c-generics-type
So, I think it is not possible to do some thing like that. |
Workaround: @interface Model: JSONModel
@property (strong, nonatomic) NSArray < Cars * > * cars;
@end
@implementation Model
- (void)setCars:(NSArray<NSDictionary*> *)cars {
NSMutableArray* mutArray = [NSMutableArray array];
for (NSDictionary* carDict in cars) {
NSError* error;
Cars* car = [[Cars alloc] initWithDictionary:carDict error:&error];
if (car && !error) {
[mutArray addObject:car];
}
}
_cars = [NSArray arrayWithArray:mutArray];
}
@end Unfortunly you need to do this with every array. |
I also tried this by adding new type annotation(with generics) with arrays, but its not giving any information about the added annotation/generics in runtime. @icanzilb , @dbachrach do you have any thoughts about this? |
Yeah, the objc generics implementation does not persist data at runtime, so it's not usable to JSONModel. |
Looks to me like it is even worse: If you do:
then Swift will map cars to [AnyObject] as Swift does not know the type. If you do:
Then swift will map it to [Cars] but JSONModel will not parse the json correctly as it does not know the type. So it seems like you can't have both. |
I turns out you can have both (I think). |
@pjebs, you can not do that, it will cause compile error. |
Fortunatelly, you can use C macro w/ setCars: workaround |
@vaddieg can you elaborate and give more details of usage? |
Very draft marco, w/o error checking #import "JSONModel.h"
#define JSONSetterImp(setterSignarure, ClassName, ivarName) \
- (void) setterSignarure :(NSArray<NSDictionary*> *)array {\
NSMutableArray *ma = [NSMutableArray arrayWithCapacity:array.count];\
for (NSDictionary* object in array) { \
ClassName *model = [[ClassName alloc] initWithDictionary:object error:NULL]; \
if (model) { \
[ma addObject:model]; \
} \
} \
ivarName = [ma copy];\
}
@interface Car : JSONModel
@end
@interface Parking : JSONModel
@property (strong, nonatomic) NSArray<Car*> *cars;
@end And Implementation of setter will look like: @implementation Parking
JSONSetterImp(setCars, Car, _cars)
@end |
I have verified that you can do this: |
@pjebs GOOD JOB. |
@pjebs Nice. Case is closed I think |
@icanzilb pls update Readme.md Model Collection example |
In the new version of Objective C (on XCode 7), we can now specify what a NSArray can contain.
Will your library support this? It will definitely reduce the amount of casting we need to do.
Say we have a JSONModel subclass called Cars.
Say we have another JSONModel subclass called Person:
@Property (strong, nonatomic) NSArray < Cars * > * cars; //Restricting NSArray to Cars
rather than currently:
@protocol Cars
@EnD
@Property (strong, nonatomic) NSArray < Cars > * cars; //Currently
The text was updated successfully, but these errors were encountered: