swift 3 issue with CVarArg being passed multiple times

前端 未结 1 1823
时光说笑
时光说笑 2021-01-19 14:37

I have below code in swift 3:

 class StringUtility {

  static func Localizer(tableName: String?) -> (_ key: String, _ params: CVarArg...) -> String {
         


        
相关标签:
1条回答
  • 2021-01-19 15:25

    You cannot pass a variable argument list to another function, you have to pass a CVaListPointer instead. Also withVaList should be used instead of getVaList:

    class StringResourceUtility {
    
        static func Localizer(tableName: String?) -> (_ key: String, _ params: CVaListPointer) -> String {
            return { (key: String, params: CVaListPointer) in
                let content = NSLocalizedString(key, tableName: tableName, comment: "")
                return NSString(format: content, arguments: params) as String
            }
        }
    }
    
    func localizationHelper(tableName: String, key: String, params: CVarArg...) -> String {
        let t = StringResourceUtility.Localizer(tableName: tableName)
        return withVaList(params) { t(key, $0) } 
    }
    

    Example:

    let s = localizationHelper(tableName: "table", key: "%@ %@", params: "Wells", "Fargo")
    print(s) // Wells Fargo
    
    0 讨论(0)
提交回复
热议问题