xcode stringWithFormat 生成带有乱码字符的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/536589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:28:45  来源:igfitidea点击:

stringWithFormat produces string with gibberish characters

iosobjective-ciphonexcodensstring

提问by TonyNeallon

When debugging the following code

调试以下代码时

NSString *var1 = @"blaa";
NSString *var2 = @"blaaaaa";

NSString* script = [NSString stringWithFormat:@"Set_Variable( %s, %s )",var1,var2]; 

the %splaceholders in script are replaced with funny gibberish characters. Can you see any errors in the code?

%s脚本中的占位符被替换为有趣的胡言乱语字符。你能看到代码中的任何错误吗?

回答by Jesús A. álvarez

%s is the format specifier for a C string, char*

%s 是 C 字符串的格式说明符,char*

For objective-c objects (such as NSString) you should use %@

对于objective-c 对象(例如NSString),您应该使用%@