Search code examples
iosobjective-cswiftobjective-c-swift-bridge

Calling Swift class method from Objective-C


I have this function in Swift

class func someFunction(idValue: Int, completionHandler: @escaping (_ jsonData: JSON) -> ()) {
        (...)
        if (some validation) {
            completionHandler(jsonData)
        } else {
            completionHandler(JSON.null)
        }

    }

Now I want to call that function from Objective-C. What I am doing is this:

[[ClassName  new] someFunction:self.sesionId completionHandler:^{
    }];

But is throwing "No visible @interface for 'ClassName' declares the selector 'someFunction:completionHandler:'

How can I call this function?


Solution

  • Essentially the NSObject is the base object type in Apple development.

    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.

    Your class defines like that, using NSObject supper class (related to your requirements).

    class ClassName: NSObject {
    
        class func someFunction(idValue: Int, completionHandler: @escaping (_ jsonData: String) -> ()) {
    
    
        }
    }
    

    call this function

    [ClassName someFunctionWithIdValue:12 completionHandler:^(NSString * test) {
    
    }];