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)}
如果在上面两个函数中看到print语句,每次在函数内部传递CVararg时都会附加[],导致输出错误的本地化字符串.
1)CVararg不能像上面的代码一样多次传递?
2)如何解决这个问题?
必须传递一个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,let s = localizationHelper(tablename: "table",key: "%@ %@",params: "Wells","Fargo")print(s) // Wells Fargo) } }
例:
总结以上是内存溢出为你收集整理的swar 3问题与CVarArg被多次传递全部内容,希望文章能够帮你解决swar 3问题与CVarArg被多次传递所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)