ios programming - Data argument not used by format string

与世无争的帅哥 提交于 2019-11-30 03:51:48

问题


I get a Data argument not used by format string error when I run the following code:

- (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:

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


回答1:


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.




回答2:


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"];

Note that I removed the parentheses for object colour.



来源:https://stackoverflow.com/questions/7886399/ios-programming-data-argument-not-used-by-format-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!