我在
swift 3中有以下代码:
class StringUtility { static func Localizer(tableName: String?) -> (_ key: String,_ params: CVararg...) -> String { return { (key: String,params: CVararg...) in let content = NSLocalizedString(key,tableName: tableName,comment: "") if params.isEmpty { return content } print(params) ->>>>>> this prints [[[Wells Fargo]]] return Nsstring.init((format: content,arguments: getVaList(params))) as String } } } func localizationHelper(tableName: String,key: String,params: CVararg...) -> String { let t = StringResourceUtility.Localizer(tableName: tableName) print(params) - >>>>>>>>>> this prints [[Wells Fargo]] return t(key,params) }
解决方法
你不能将变量参数列表传递给另一个函数
必须传递一个CVaListPointer.还有withVaList应该
用来代替getVaList:
必须传递一个CVaListPointer.还有withVaList应该
用来代替getVaList:
class StringResourceUtility { static func Localizer(tableName: String?) -> (_ key: String,_ params: CVaListPointer) -> String { return { (key: String,params: CVaListPointer) in let content = NSLocalizedString(key,comment: "") return Nsstring(format: content,arguments: params) as String } } } func localizationHelper(tableName: String,params: CVararg...) -> String { let t = StringResourceUtility.Localizer(tableName: tableName) return withVaList(params) { t(key,$0) } }
例:
let s = localizationHelper(tableName: "table",key: "%@ %@",params: "Wells","Fargo") print(s) // Wells Fargo