ios ios编程 - 格式字符串未使用数据参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7886399/
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
ios programming - Data argument not used by format string
提问by gadgetmo
I get a Data argument not used by format string
error when I run the following code:
Data argument not used by format string
运行以下代码时出现错误:
- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
NSString *colour = ([colourArray objectAtIndex:row]);
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:(colour) forKey:@"colour"];
NSLog(@"NSString =", colour);
NSLog(@"NSUserDefaults =", [defaults objectForKey:@"colour"]);
}
I get the error on both NSLog
lines. Also, here is what the log says:
我在两条NSLog
线上都收到错误消息。另外,这里是日志所说的:
2011-10-25 09:01:50.260 Random[35636:b303] NSString =
2011-10-25 09:01:50.260 Random[35636:b303] NSUserDefaults =
Thank you, Arthur
谢谢你,亚瑟
回答by zakishaheen
NSLog(@"NSString = ", colour);
NSLog(@"NSUserDefaults =", [defaults objectForKey:@"colour"]);
Is problematic
有问题
Should be
应该
NSLog(@"NSString = %@", colour);
NSLog(@"NSUserDefaults = %@", [defaults objectForKey:@"colour"]);
The format specifier in this case is the %@
which is used to print an object
. To print numbers you'd use something like %d
. See complete documentation here.
在这种情况下,格式说明符%@
是用于打印object
. 要打印数字,您可以使用类似%d
. 在此处查看完整文档。
回答by Jorge Ramos
Answer from @debuggerman is absolutely correct. But you can improve your code if you use [defaults setObject:colour forKey:@"colour”];
instead of [defaults setObject:(colour) forKey:@"colour"];
@debuggerman 的回答是绝对正确的。但是如果你使用[defaults setObject:colour forKey:@"colour”];
而不是你可以改进你的代码[defaults setObject:(colour) forKey:@"colour"];
Note that I removed the parentheses for object colour
.
请注意,我删除了 object 的括号colour
。