I’ve been pulling my hair on a weird bug in Xcode 10.2. I’ve had this swift util method to decode JSON objects using ObjectMapper as I can’t call ObjectMapper from ObjectiveC:
@objc static func mapModel(fromClass: AnyClass, jsonDict: [String : Any]) -> Any? {
guard let mappableClass = fromClass as? Mappable.Type else {
fatalError("class \(fromClass) is not Mappable")
}
return mappableClass.init(JSON: jsonDict)
}
Here's how I call it in ObjectiveC:
FullUser *object = [ModelMapperHelper mapModelFromClass:FullUser.class jsonDict:response.resultData];
I had to use AnyClass
because Mappable
is not compatible with ObjC.
This code has been working fine until I updated to Xcode 10.2.
The last line in the thread in the debugger is swift_checkMetadataState
It’s still working fine with devices on iOS 12.2 and above but it’s crashing with a EXC_BAD_ACCESS
at the init()
line on iOS 12.1 and below.
If I use the actual class like FullUser.init(JSON: jsonDict)
it's all working fine.
Another interesting thing, if I don't pass FullUser.class
from objc but set it in swift like so:
@objc static func mapFullUser(jsonDict: [String : Any]) -> Any {
let fromClass = FullUser.self
let mappableClass = fromClass as Mappable.Type
return mappableClass.init(JSON: jsonDict)!
}
it all works fine.
Does anyone have any idea why it’s crashing on older iOS versions and how I can change that code?
Update 1
By just adding print("swift=\(FullUser.self)")
before the init()
call it's working fine!
I'm starting to think this is a class loader issue. Maybe it has to do with the fact that the project has 2 targets.
Update 2
I removed the 2nd target and it's still crashing.
Update 3
It also crashes if I just pass the class name as a string and get the class from NSClassFromString
in swift.
Update 4
It doesn't crash if I decode the parent class User
which FullUser
inherits.
@objc(User)
class User: NSObject, Mappable {
@objc(FullUser)
class FullUser: User {
Update 5
I tried to call a different static function than init
and it's not crashing.