EXC_BAD_ACCESS error on @escaping completion handler when called from Objective C code

Viewed 139

I have a Swift function public func doSomething( aKey : String, completed: @escaping (AModel?, TagError?)->()) {} that needs to be exposed to Objective C code for consumption. I have created an Objective C class wrapper like

@objc
public func doSomethingObjCWrapper(aKey : String) {
       anObject.doSomething(aKey: aKey) { (modelA, error) in
            if let whtModel = modelA {
                // All good

                DispatchQueue.main.async {
                    print("ok")
                }

            } else {
                print("\(error?.localizedDescription ?? "Unknown error")")
            }
        } 
}

to be called from Objective C code. Whenever the code gets triggered, I will always get EXC_BAD_ACCESS error in anObject.doSomething line. Any lead will be much appreciated.

1 Answers

is anObject conform the NSObject protocol?

if not, you can try this way because of the root class of most Objective-C class hierarchies, from which subclasses inherit a basic interface to the runtime system and the ability to behave as Objective-C objects

Reference https://developer.apple.com/documentation/objectivec/nsobject

Related