Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call variadic Objective-C function from Swift

I have summarized the steps of the problem, I have a C function defined in Objective-C:

ObjC.h

#import <Foundation/Foundation.h>

void cuslog(NSString *format, ...);

@interface ObjC : NSObject

@end

ObjC.m

#import "ObjC.h"

@implementation ObjC

@end

void cuslog(NSString *format, ...)
{
     // Implementation
}

I exposed it in Bridging-Header.h:

#import "ObjC.h"

// Also tried to put this line in bridging header
void cuslog(NSString *format, ...);

In Swift I intend to call the function like this:

cuslog("Some log")

But the error says:

"Use of unresolved identifier 'cuslog'"

What is the correct way to call the function in Swift?

like image 262
vladof81 Avatar asked Jun 23 '14 19:06

vladof81


1 Answers

According to the Swift devs, C variadic functions are not compatible with Swift variadics, so you won't be able to call your function directly.

The only workaround at this time is to write a non-variadic wrapper in C or Obj-C, and call that from Swift.

like image 177
jatoben Avatar answered Sep 25 '22 03:09

jatoben