具有未知参数的Objective-c方法

时间:2023-01-15 18:29:47

I am writing a category against NSMutableString that uses appendFormat: internally.

我正在针对使用appendFormat: internal的NSMutableString编写一个类别。

@interface NSMutableString (Additions)    
- (void)appendFormatWithLine:(NSString *)format, ...;    
@end

@implementation NSMutableString (Additions)

- (void)appendFormatWithLine:(NSString *)format, ... {
    va_list args;
    va_start(args, format);

    // **calling [self appendFormat] gives a 'EXC_BAD_ACCESS'**
    [self appendFormat:format, args];
    [self appendString:@"\r\n"];

    va_end(args);
}

@end

I have no idea how to use va_list or va_start to call NSMutableString:appendFormat. How can this be done?

我不知道如何使用va_list或va_start调用NSMutableString:appendFormat。怎么做呢?

2 个解决方案

#1


5  

Well yeah. -appendFormat: takes variadic arguments, not a variadic argument list (va_list.) There's no single method on NSMutableString that takes an argument list, so you should do this instead:

嗯是的。-appendFormat:取可变参数,而不是可变参数列表(va_list)。在NSMutableString上没有一个方法可以获取参数列表,所以你应该这样做:

NSString *formatted = [[NSString alloc] initWithFormat: format arguments: args];
if (formatted) {
    [self appendString: formatted];
    [formatted release];
}

#2


4  

You can't do this. appendFormat: accepts variadic arguments (the arguments themselves), NOT a va_list structure. In C, it's impossible to pass down variadic arguments.

你不能这么做。appendFormat:接受可变参数(参数本身),而不是va_list结构。在C中,不可能传递变量参数。

Your method, however, can be rewritten like this:

然而,你的方法可以这样重写:

- (void)appendFormatWithLine:(NSString *)format, ...
{
    va_list args;
    va_start(args, format);

    // **calling [self appendFormat] gives a 'EXC_BAD_ACCESS'**
    NSString *formatString = [[NSString alloc] initWithFormat:format arguments:args];
    [self sppendString:formatString];
    [formatString release];
    [self appendString:@"\r\n"];

    va_end(args);
}

#1


5  

Well yeah. -appendFormat: takes variadic arguments, not a variadic argument list (va_list.) There's no single method on NSMutableString that takes an argument list, so you should do this instead:

嗯是的。-appendFormat:取可变参数,而不是可变参数列表(va_list)。在NSMutableString上没有一个方法可以获取参数列表,所以你应该这样做:

NSString *formatted = [[NSString alloc] initWithFormat: format arguments: args];
if (formatted) {
    [self appendString: formatted];
    [formatted release];
}

#2


4  

You can't do this. appendFormat: accepts variadic arguments (the arguments themselves), NOT a va_list structure. In C, it's impossible to pass down variadic arguments.

你不能这么做。appendFormat:接受可变参数(参数本身),而不是va_list结构。在C中,不可能传递变量参数。

Your method, however, can be rewritten like this:

然而,你的方法可以这样重写:

- (void)appendFormatWithLine:(NSString *)format, ...
{
    va_list args;
    va_start(args, format);

    // **calling [self appendFormat] gives a 'EXC_BAD_ACCESS'**
    NSString *formatString = [[NSString alloc] initWithFormat:format arguments:args];
    [self sppendString:formatString];
    [formatString release];
    [self appendString:@"\r\n"];

    va_end(args);
}