0

I use the following code

myFunc(code:String) {        
  let t1f = NSLocalizedString("uiDlg Title code %s", comment: "uiDlg Title");        
  let t1 = String.localizedStringWithFormat(t1f, code);

The string is declared in Localizable.strings file as

"uiDlg Title code %s" = "code [%s]";

if I call myFunc("112233") the result string on the screen contains strange characters as

code [Ä&:#] 

if I use

let t1 = String.localizedStringWithFormat(t1f, code) + code; 

The second code is displayed properly as

code [Ä&:#] 112233

Do I need to use %s as the format specifier for string? I could not find such sample code, all samples contains %d or %f for number formatting...

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
Atara
  • 3,523
  • 6
  • 37
  • 56
  • as a programmer new to swift, with C habbits, %s seems good enoth for my purpose. I did not know there is a difference between 'null terminated C string' and 'Swift string' – Atara Jan 13 '20 at 11:51

1 Answers1

4

%s is for a null terminated C string, the specifier for a standard Swift string is %@.

You can find the list of format specifiers in the documentation

vadian
  • 274,689
  • 30
  • 353
  • 361