传递可变数量的参数时出现Swift问题
我有以下代码:传递可变数量的参数时出现Swift问题,swift,arguments,Swift,Arguments,我有以下代码: 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 pa
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
}
我在NSLog上遇到编译器错误,无法像我那样调用
只需使用单个NSLOG调用打印var args、函数名和行。与中类似,
您必须创建一个CVaListPointer,它相当于C中的va_list,并调用一个函数,该函数接受一个CVaListPointer参数,
在这种情况下,NSLogv
我将format作为第一个参数,因此它没有外部参数
参数名。变量参数列表必须是最后一个参数
在Swift 1.2中,外部参数是无法避免的
与默认参数的组合
在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")
类似于,
您必须创建一个CVaListPointer,它相当于C中的va_list,并调用一个函数,该函数接受一个CVaListPointer参数,
在这种情况下,NSLogv
我将format作为第一个参数,因此它没有外部参数
参数名。变量参数列表必须是最后一个参数
在Swift 1.2中,外部参数是无法避免的
与默认参数的组合
在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")
当前的Xcode 7.3语法是文件、函数、行和谢谢当前的Xcode 7.3语法是文件、函数、行和谢谢