2

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?

jscs
  • 63,694
  • 13
  • 151
  • 195
martinc
  • 89
  • 2
  • 11

2 Answers2

2

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) {

}];
AshvinGudaliya
  • 3,234
  • 19
  • 37
  • 8
    Using **Swift 4** I had to add `@objc` to the function declaration to make the compiler recognize the Swift class function in my Objective-C code: `@objc class func someFunction(idValue: Int, completionHandler: @escaping (_ jsonData: String) -> ())` – bfx Jan 18 '18 at 11:47
0

In addition to inheriting from NSObject and declaring the class with @objc , Seems that @objc must be added to the func declaration for it to be seen in objective-c classes/files.

like so:

@objc class SomeClass:NSObject {


 @objc func someFunction(_:Any) -> Bool {  
        return true
       }

}

It then shows up in the autocomplete when calling as normal :

#import "PROJECT_Name-Swift.h"
@implementation SomeObjCClass
...


-(void)someObjCMethod{

    SomeClass* instance = [[SomeClass alloc]init];

    [instance someFunction:(id _Nonnull)]
}
...
@end

the presence of public keyword didn't seem to affect it either way

SmokeyTBear
  • 129
  • 1
  • 6