0

My app will be used in different countries, so we get localization parameters from API. For strings which have not got any parameters like %@, %d etc, there is no problem. But I cannot find any solution for a string with parameters.

For instance, is the service give me string like

"Your entered email must be at least %d character and maximum %d character" or it will have %@ parameters.

On a client, I know the giving parameters number because ı know its usage label or text field but cannot do any algorithm like String with format method.

[NSString stringWithFormat:[string from service metod],%d,%d]; ---- xcode gives error for %d..
Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50
  • Give a real example of what you are trying to achieve, ur code is just plain wrong and I cannot understand what you need – Tj3n Feb 05 '18 at 05:27
  • Ok. For example the method is mine service Utils methos and it gives me a string value of the key. "[[MMMUtility sharedManager] getValueByKey:email_error]" ----- From the service, email_error:"Lütfen e-posta adresinizi en az %d ve en fazla %d karakter olarak giriniz.". I know the value of key contains 2 parameters. But I cannot write like that -------NSString *result = [NSString stringWithFormat"[[MMMUtility sharedManager] getValueByKey:email_error], var1,var2];--- Can I explain? @Tj3n –  Feb 05 '18 at 05:32
  • this may be [help](https://stackoverflow.com/questions/4881628/how-do-i-localize-a-string-with-formatting-placeholders) – Rocky Feb 05 '18 at 06:43
  • The code you show in the question itself gives an error as `%d` is not a valid expression (it only has meaning as part of a format string). However the code you show in your comment, apart from the minor typo (" instead of :), is in the correct form. You say this gives an error but do not say *what* error. Edit your question to include both the code in the comment and the error you get and that will probably enable someone to help you out. – CRD Feb 05 '18 at 20:40

1 Answers1

0

Try this.

NSString *result = [NSString stringWithFormat:@"string from service method %d %d", var1,var2];
  • :) I know this code also. But I dont have any static string. It is given me by service. –  Feb 05 '18 at 05:31
  • so, you can use [[MMMUtility sharedManager] getValueByKey:email_error] instead of var. – Md. Mostafizur Rahman Feb 05 '18 at 05:34
  • But the method gives error when I write ---------NSString *result = [NSString stringWithFormat:[[MMMUtility sharedManager] getValueByKey:email_error], var1,var2];---- because Xcode can not put var1 and var2 for anything. –  Feb 05 '18 at 05:38
  • @BegümKırkgözDemirci Why error with this? You just need to make `int var1` without value and compiler will stop saying error? – Tj3n Feb 05 '18 at 05:44
  • First of all, your question was not clear. So, if you have a string with two numbers, then parse this two number from the string. If you still face any problem, let me know. – Md. Mostafizur Rahman Feb 05 '18 at 05:53