Isses using function with variadic arguments

Posted by Sausages on Stack Overflow See other posts from Stack Overflow or by Sausages
Published on 2014-06-11T21:21:22Z Indexed on 2014/06/11 21:24 UTC
Read the original article Hit count: 129

Filed under:

I'm trying to write a logging function and have tried several different attempts at dealing with the variadic arguments, but am having problems with all of them. Here's the latest:

- (void) log:(NSString *)format, ...
{
    if (self.loggingEnabled)
    {
        va_list vl;
        va_start(vl, format);
        NSString* str = [[NSString alloc] initWithFormat:format arguments:vl];
        va_end(vl);
        NSLog(format); 
    }
}

If I call this like this:

[self log:@"I like: %@", @"sausages"];

Then I get an EXC_BAD_ACCESS at the NSLog line (there's also a compiler warning that the format string is not a string literal).

However if in XCode's console I do "po str" it displays "I like: sausages" so str seems ok.

© Stack Overflow or respective owner

Related posts about objective-c