I have the following code:
我有以下代码:
public static func e(file: String = __FILE__, function: String = __FUNCTION__, line: Int = __LINE__,format: String, args: CVarArgType...){ NSLog([%d]\t [%@] " + format,line,function, args); //<<< I have no idea how to pass the params here}
I get compiler error on NSLog that can not be called as I did.
我在NSLog上遇到了无法像我那样调用的编译器错误。
Simply I need to print the var args, function name and line using a single NSLOG call.
我只需要使用单个NSLOG调用打印var args,函数名称和行。
1 个解决方案
#1
3
Similar as in Swift function with args... pass to another function with args,you have to create a CVaListPointer
(the Swift equivalent of va_list
in C) and call a function which takes a CVaListPointer
parameter,in this case NSLogv()
.
类似于带有args的Swift函数...使用args传递给另一个函数,你必须创建一个CVaListPointer(相当于C中的va_list的Swift)并调用一个带有CVaListPointer参数的函数,在本例中为NSLogv()。
public class Logger { public static func e( format: String, file: String = __FILE__, function: String = __FUNCTION__, line: Int = __LINE__, args: CVarArgType...) { let extendedFormat = "[%d]\t [%@] " + format let extendedArgs : [CVarArgType] = [ line, function ] + args withVaList(extendedArgs) { NSLogv(extendedFormat, $0) } }}// Example usage:Logger.e("x = %d, y = %f, z = %@", args: 13, 1.23, "foo")
I have made format
the first parameter, so that is has no externalparameter name. The variable argument list must be the last parameterin Swift 1.2, and the external parameter cannot be avoided in thiscombination with default parameters.
我已经格式化了第一个参数,因此没有外部参数名称。变量参数列表必须是Swift 1.2中的最后一个参数,并且在与默认参数的组合中无法避免外部参数。
In Swift 2 you can avoid the external parameter name for thevariable arguments:
在Swift 2中,您可以避免变量参数的外部参数名称:
public class Logger { public static func e( format: String, _ args: CVarArgType..., file: String = __FILE__, function: String = __FUNCTION__, line: Int = __LINE__) { let extendedFormat = "[%d]\t [%@] " + format let extendedArgs : [CVarArgType] = [ line, function ] + args withVaList(extendedArgs) { NSLogv(extendedFormat, $0) } }}// Example usage:Logger.e("x = %d, y = %f, z = %@", 13, 1.23, "foo")
#1
3
Similar as in Swift function with args... pass to another function with args,you have to create a CVaListPointer
(the Swift equivalent of va_list
in C) and call a function which takes a CVaListPointer
parameter,in this case NSLogv()
.
类似于带有args的Swift函数...使用args传递给另一个函数,你必须创建一个CVaListPointer(相当于C中的va_list的Swift)并调用一个带有CVaListPointer参数的函数,在本例中为NSLogv()。
public class Logger { public static func e( format: String, file: String = __FILE__, function: String = __FUNCTION__, line: Int = __LINE__, args: CVarArgType...) { let extendedFormat = "[%d]\t [%@] " + format let extendedArgs : [CVarArgType] = [ line, function ] + args withVaList(extendedArgs) { NSLogv(extendedFormat, $0) } }}// Example usage:Logger.e("x = %d, y = %f, z = %@", args: 13, 1.23, "foo")
I have made format
the first parameter, so that is has no externalparameter name. The variable argument list must be the last parameterin Swift 1.2, and the external parameter cannot be avoided in thiscombination with default parameters.
我已经格式化了第一个参数,因此没有外部参数名称。变量参数列表必须是Swift 1.2中的最后一个参数,并且在与默认参数的组合中无法避免外部参数。
In Swift 2 you can avoid the external parameter name for thevariable arguments:
在Swift 2中,您可以避免变量参数的外部参数名称:
public class Logger { public static func e( format: String, _ args: CVarArgType..., file: String = __FILE__, function: String = __FUNCTION__, line: Int = __LINE__) { let extendedFormat = "[%d]\t [%@] " + format let extendedArgs : [CVarArgType] = [ line, function ] + args withVaList(extendedArgs) { NSLogv(extendedFormat, $0) } }}// Example usage:Logger.e("x = %d, y = %f, z = %@", 13, 1.23, "foo")