运行以下代码时出现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"]);
}
我在NSLog
行都收到错误。此外,这是日志所说的内容:
2011-10-25 09:01:50.260 Random[35636:b303] NSString =
2011-10-25 09:01:50.260 Random[35636:b303] NSUserDefaults =
谢谢你, 亚瑟
答案 0 :(得分:20)
NSLog(@"NSString = ", colour);
NSLog(@"NSUserDefaults =", [defaults objectForKey:@"colour"]);
有问题
应该是
NSLog(@"NSString = %@", colour);
NSLog(@"NSUserDefaults = %@", [defaults objectForKey:@"colour"]);
本案例中的格式说明符是%@
,用于打印object
。要打印数字,您可以使用%d
之类的内容。 See complete documentation here.
答案 1 :(得分:0)
来自@debuggerman的回答绝对正确。但是,如果您使用[defaults setObject:colour forKey:@"colour”];
代替[defaults setObject:(colour) forKey:@"colour"];
请注意,我删除了对象colour
的括号。