Trouble displaying % in NSRunAlertPanel
I am developing an Desktop application where I want to show a message in alert panel using NSRunAlertPanel. I am doing the following things:
NSString *title = @"% Test";
NSString *message = @"% Test Message";
NSRunAlertPanel(title, message, @"Ok" ,@"Cancel" ,nil);
The alert panel show the title properly. i.e % Test But, the message开发者_高级运维 is est Message; I want to display % Test Message.
How do I solve this problem?
Thanks in advance.
Try this :
NSString *title = @"% Test";
NSString *message = @"%% Test Message";
NSRunAlertPanel(title, message, @"Ok" ,@"Cancel" ,nil);
Why?
NSRunAlertPanel
uses NSBeginAlertSheet
. Looking at the documentation for NSBeginAlertSheet you can see that there are more parameters after msg (specified by the ...
).
This tells us that title is just a string literally displayed but the message can have format parameters the same way that [NSString stringWithFormat:]
does.
The way that a string specifies that there is going to be a parameter is by using a % character i.e. %i means 'put an integer here', %@ means 'put an object's description here'. You've just put a % by itself, which gets things very confused!
The double %% means this percent isn't me telling you that I want you to put anything special in there, I really just want the % please.
精彩评论