Vizllx Vizllx - 1 year ago 147
iOS Question

Call Swift completion handler in objective c

I am trying to call a swift method, which is implemented like this:-

@objc class DataAPI: NSObject {
func makeGet(place:NSString , completionHandler: (String! , Bool!) -> Void)
var str:String = ""
let manager = AFHTTPSessionManager()

manager.GET("", parameters: nil, success:
{ (operation, responseObject) -> Void in
str = "JSON: \(responseObject!.description)"

completionHandler(str,false) //str as response json, false as error value

failure: { (operation,error: NSError!) in
str = "Error: \(error.localizedDescription)"


Now when I am trying to call it in my Objective C class, it is throwing an error "No Visible interface for DataAPI declares selector makeGet:completionHandler"

This is how I am calling the method in my Objective C class:-

[[DataAPI new] makeGet:@"" completionHandler:^{

Answer Source

You shouldn't use !(ImplicitUnwrappedOptional) keyword in closure. That is not allow bridging to ObjC code. just remove ! from closure.

func makeGet(place:NSString , completionHandler: (String! , Bool!) -> Void)


func makeGet(place:NSString , completionHandler: (String , Bool) -> Void)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download