Skip to content
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

is FastEasyMapping ready for swift 4? #116

Open
surkis opened this issue Aug 29, 2018 · 3 comments
Open

is FastEasyMapping ready for swift 4? #116

surkis opened this issue Aug 29, 2018 · 3 comments

Comments

@surkis
Copy link

surkis commented Aug 29, 2018

I have iOS project with swift 3.x, and trying to migrate it to latest swift 4.x
I have setuped FastEasyMapping via Cocoapods like this:
pod 'FastEasyMapping', :git => 'https://github.com/Yalantis/FastEasyMapping.git', :branch => 'required-pk'

But I have next crash issue when try to serialize/deserialize objects:
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<fasteasymappingtest.SGSignInEntity 0x604000151b40> valueForUndefinedKey:]: this class is not key value coding-compliant for the key email.' *** First throw call stack: ( 0 CoreFoundation 0x000000010e6dc1e6 __exceptionPreprocess + 294 1 libobjc.A.dylib 0x000000010afb1031 objc_exception_throw + 48 2 CoreFoundation 0x000000010e6dc0b9 -[NSException raise] + 9 3 Foundation 0x000000010aa9ef58 -[NSObject(NSKeyValueCoding) valueForUndefinedKey:] + 226 4 Foundation 0x000000010a9caebc -[NSObject(NSKeyValueCoding) valueForKey:] + 284 5 FastEasyMapping 0x00000001098533fc -[FEMSerializer setValueOnRepresentation:fromObject:withFieldMapping:] + 156 6 FastEasyMapping 0x000000010985295f -[FEMSerializer _serializeObject:usingMapping:] + 463 7 FastEasyMapping 0x0000000109852c92 -[FEMSerializer serializeObject:usingMapping:] + 114 8 FastEasyMapping 0x0000000109853fe0 +[FEMSerializer(Shortcut) serializeObject:usingMapping:] + 144

In this method:
- (void)setValueOnRepresentation:(NSMutableDictionary *)representation fromObject:(id)object withFieldMapping:(FEMAttribute *)fieldMapping { id returnedValue = [object valueForKey:fieldMapping.property]; if (returnedValue || self.includeNulls) { returnedValue = [fieldMapping reverseMapValue:returnedValue] ?: [NSNull null]; [self setValue:returnedValue forKeyPath:fieldMapping.keyPath inRepresentation:representation]; } }

object can not return valueForKey, because doesn't casted to valid class.
I think it's some changes in Swift 4 and Objective-C relation.

Please provide any advise how to fix it? I am using your library in many places, and can not rewrite it to other libraries easy.

@surkis
Copy link
Author

surkis commented Aug 29, 2018

Attached archive of test project, so you can test problem yourself:
fasteasymappingtest.zip

When I changed to swift 3.3, in target Build settings, all working as expected.
If I using to swift 4.1, I have this issue.

@dimazen
Copy link
Contributor

dimazen commented Aug 29, 2018

@surkis thank you so much for provided feedback. I would assume, that this is because in Swift 4 behaviour of swift-objc interoperability has been changed. Therefore subclassing from NSObjectdoesn't reveal class members / methods to ObjC automatically. You have to add attributes @objc dynamic for each property you'd like to use with KVC. FEM uses KVC internally and therefore since those properties are not attributed correctly, it can't find them via KVC.

@surkis surkis changed the title is FastEasyMapping ready for swift4? is FastEasyMapping ready for swift 4? Aug 29, 2018
@surkis
Copy link
Author

surkis commented Aug 29, 2018

@dimazen thank you for such fast feedback.
Yes, after I have added @objc dynamic to each attributes in class, it's start working as expected in test project.
I have attached working version here, just for reference who will have same issue:
fasteasymappingtestswift4.zip

Thank you for your help. I will continue with migration in my project tomorrow, and if I will find something new, will contact with you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants