3

I have the next function in objective c

+ (NSString *)getNsLog:(NSString *)pString, ...{
 va_list args;
 va_start(args, pString);
 NSLogv(pString, args);
 va_end(args);
 return [[NSString alloc] initWithFormat:pString arguments:args]; 
}

how can I call this function from swift or convert the code to swift such that when I call the function can be:

getNslog("my value1 = %@ value2 = %@","hello","world")

Note that the second param not have alias how this.

getNslog("my value1 = %@ value2 = %@", args:"hello","world")
Pang
10.2k146 gold badges87 silver badges126 bronze badges
asked Jun 18, 2015 at 16:31
1

2 Answers 2

3

I solved follow:

in objective c change my code to this:

+(NSString*)getNsLog:(NSString*)pString args:(va_list)args{
NSLogv(pString, args);
va_end(args);
return [[NSString alloc] initWithFormat:pString arguments:args];
}

in swift app delegate I add

extension MyClass {
class func getNsLog(format: String, _ args: CVarArgType...) -> NSString?
 {
 return MyClass.getNsLog(format, args:getVaList(args))
 }
}

now I can call the function

NSLog("%@", MyClass.getNsLog("%@,%@", "hello","World")!)

I based in the post duplicated How do you call an Objective-C variadic method from Swift?

thanks.

answered Jun 18, 2015 at 19:02
Sign up to request clarification or add additional context in comments.

Comments

-1
  1. Simply import your class containing this method in YourProjectname-Bridging-Header.h file.

     #import "`YourClass.h"
    
  2. Create imported class's object

     var a = YourClass()
    
  3. then simply call the method and pass required parameter

    a.getNsLog(yourParameters)
    

I hope this helps

answered Jun 18, 2015 at 16:42

1 Comment

I make it but only this function not is recognized

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.