5

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?

jscs
  • 63,694
  • 13
  • 151
  • 195
vladof81
  • 26,121
  • 9
  • 38
  • 41
  • 2
    Possible duplicate of [How do you call an Objective-C variadic method from Swift?](https://stackoverflow.com/questions/24195796/how-do-you-call-an-objective-c-variadic-method-from-swift) – jscs Jul 26 '17 at 21:26

1 Answers1

5

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.

jatoben
  • 3,079
  • 15
  • 12
  • 1
    Plus, any decent API with C/Obj-C variadic functions should have a non-variadic version that takes a `va_list` argument, or a way to add elements one by one. Either of these are callable from Swift. – newacct Jun 24 '14 at 01:11