swar 3问题与CVarArg被多次传递

我在 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)
}

如果在上面两个函数中看到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,$0) } 
}

例:

let s = localizationHelper(tableName: "table",key: "%@ %@",params: "Wells","Fargo")
print(s) // Wells Fargo

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...