0

I have a method written in Objective-C, which I am replacing with Swift code. But, I do not want to change the method signature as it is used in number of places. I am writing a new version of it in Swift with same signature, which should be called from Obj-C code.

This is the method. +(void)printLog:(NSString *)msgToPrint, ...;

I want a Swift version of this method, which will be called from Obj-C code. Thanks in advance for the help.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 1
    If you want to call Obj-C variadic function, see _Variadic Functions_ in [Interacting with C APIs: Functions](https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/InteractingWithCAPIs.html#//apple_ref/doc/uid/TP40014216-CH8-ID207). If you just want to write your own Swift variadic function, see _Variadic Parameters_ section of [The Swift Programming Language: Functions](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html#//apple_ref/doc/uid/TP40014097-CH10-ID166). – Rob Sep 21 '17 at 05:44
  • Or, depending upon what you're doing in this function, this might be a good time to consider transitioning to [Unified Logging and Activity Tracing](https://developer.apple.com/videos/play/wwdc2016/721/), which offers a lot of advantages over traditional logging mechanisms. – Rob Sep 21 '17 at 05:50

1 Answers1

0

That would be the class method like this

class func printLog(msgToPrint: String, _ args: CVarArg...) -> Void
{

}
Hwangho Kim
  • 629
  • 4
  • 20
  • Yes it will be a class function in Swift. But, the problem here is of variadic parameters. If you observe the method signature the last parameter is variadic. Thats what is causing the problem. Though I define the function but its not recognized in Obj-C code of variadic parameter. – Swift Technocrat Sep 21 '17 at 05:35
  • I updated my answer and please have a look this site https://stackoverflow.com/questions/24195796/how-do-you-call-an-objective-c-variadic-method-from-swift – Hwangho Kim Sep 21 '17 at 05:41