开发者

NSString stringWithFormat "%" "d" 4 instead of "%d" 4?

开发者 https://www.devze.com 2023-03-23 08:18 出处:网络
I want to do the follo开发者_如何学Gowing where \"%d\",9 is done joining \"%\",\"d\" <--- there is a reason to break up, to loop, ie:%x %d %g %f where I loop just \"x d g f\"

I want to do the follo开发者_如何学Gowing where "%d",9 is done joining "%","d" <--- there is a reason to break up, to loop, ie: %x %d %g %f where I loop just "x d g f"

lbl.text =[NSString stringWithFormat:@"%@%@", @"%", @"d", 9]; <<--errors
  • the desired output is "9" <-----

Thanks


Only the first argument is the format, so you need another stringWithFormat: to take the generated format string. So something like:

bl.text = [NSString stringWithFormat:[NSString stringWithFormat:@"%@%@", @"%", @"d"], 9];
                                                               // ^ OR @"%%%@", @"%d" if only the 'd' is variable

Although it seems like an NSScanner or -[NSNumber stringValue] or something would be better suited to this kind of task.

0

精彩评论

暂无评论...
验证码 换一张
取 消