iosobjective-cnsstringargumentsstringwithformat

How to pass NSString as arguments with formatWithString?


This is a fundamental part of my app, infact it is a function.

-(void)appendArguments:(NSString *)argument {
NSString *temp = [NSString stringWithFormat:_path, argument;
}

But this is not possible. Here _path is an instance variable that holds a string with integer arguments. The string argument holds integers, for example:

_path = @"I got %d apples, ate %d, I am left with %d";
argument = @"7,3,7-3";
*temp = [NSString stringWithFormat:_path, argument;

Can we make an array of arguments as objects, and replace them with pointer argument.

My app is now on hold because of this.


Solution

  • You can use the va_list.

    void myLog(NSString *format, ...)
    {
    #ifdef DEBUG
        va_list ap;
        va_start(ap, format);
        NSString *string = [[NSString alloc] initWithFormat:format arguments:ap];
        fprintf(stdout, "%s", string.UTF8String);
        va_end(ap);
    #endif
    }
    
    
    @implementation MyViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        myLog(@"%s, %@\n", __func__, self);
    }