FastEasyMapping icon indicating copy to clipboard operation
FastEasyMapping copied to clipboard

is FastEasyMapping ready for swift 4?

Open surkis opened this issue 7 years ago • 3 comments

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 avatar Aug 29 '18 14:08 surkis

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.

surkis avatar Aug 29 '18 15:08 surkis

@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.

dimazen avatar Aug 29 '18 15:08 dimazen

@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.

surkis avatar Aug 29 '18 15:08 surkis