NSLog和NSLogv之间的区别 [英] Difference between NSLog and NSLogv

查看:595
本文介绍了NSLog和NSLogv之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释NSLog和NSLogv之间的区别吗?我知道NSLog用于在控制台中打印数据.但是什么是NSLogv?

Can anyone explain the difference between NSLog and NSLogv? I know NSLog is used to print data in the console. But what is NSLogv?

推荐答案

假设您要编写类似于 NSLog的函数,但是除了记录消息外,它还可以将消息保存到数组中.您将如何实施?

Suppose you want to write a function similar to NSLog, but which also saves the message to an array in addition to logging it. How would you implement this?

如果您编写 variadic函数 void MySpecialLog(NSString *format, ...),则有人可以调用您的函数类似于NSLog — MySpecialLog(@"Hello %@!", name); —但访问format之外的其他参数的唯一方法是使用 splat运算符在C或Obj-C中,可以将它们直接传递给函数内部的NSLog.

If you write a variadic function void MySpecialLog(NSString *format, ...), someone can call your function just like NSLog — MySpecialLog(@"Hello %@!", name); — but the only way to access the extra arguments beyond format is with a va_list. There's no splat operator in C or Obj-C allowing you to pass them directly to NSLog inside the function.

NSLogv通过通过va_list一次接受所有其他参数来解决此问题.其签名为void NSLogv(NSString *format, va_list args).您可以使用它构建自己的 NSLog包装器.

NSLogv solves this problem by accepting all the additional arguments at once via a va_list. Its signature is void NSLogv(NSString *format, va_list args). You can use it to build your own NSLog wrappers.

void MySpecialLog(NSString *format, ...)
  NS_FORMAT_FUNCTION(1, 2)
    // The NS_FORMAT_FUNCTION attribute tells the compiler to treat the 1st argument like
    // a format string, with values starting from the 2nd argument. This way, you'll
    // get the proper warnings if format specifiers and arguments don't match.
{
    va_list args;
    va_start(args, format);

    // Do something slightly more interesting than just passing format & args through...
    NSString *newFormat = [@"You've called MySpecialLog()! " stringByAppendingString:format];

    NSLogv(newFormat, args);

    va_end(args);
}

您甚至可以使用相同的技术通过Obj-C方法包装NSLog. (而且,由于-[NSString initWithFormat:]有一个类似的变体,称为-initWithFormat:arguments:,因此您也可以将其包装起来.)

You can even use the same technique to wrap NSLog with an Obj-C method. (And since -[NSString initWithFormat:] has a similar variant called -initWithFormat:arguments:, you can wrap it too.)

- (void)log:(NSString *)format, ... NS_FORMAT_FUNCTION(1, 2)
{
    // Similarly to the above, we can pass all the arguments to -initWithFormat:arguments:.
    va_list args;
    va_start(args, format);
    NSString *message = [[NSString alloc] initWithFormat:format arguments:args];
    va_end(args);

    // Why not both?
    va_start(args, format);
    NSLogv(format, args);
    va_end(args);
}


迅速

在Swift中,您可以使用接受CVarArg...的可变参数来完成此操作:


Swift

In Swift, you can do this with a variadic function accepting CVarArg...:

func mySpecialLog(_ format: String, _ args: CVarArg...) {
    withVaList(args) {
        NSLogv("You've called mySpecialLog()! " + format, $0)
    }
}

这篇关于NSLog和NSLogv之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆