xcode NSInvalidArgumentException,零参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12888703/
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
NSInvalidArgumentException, nil argument
提问by user1678809
I've looked over the site for a while and I saw a few things but nothing worked for me. Extreme newbie here though.
我已经浏览了该网站一段时间,我看到了一些东西,但没有任何效果对我有用。虽然这里是极端的新手。
I'm trying to make a 2 component picker, populated from arrays, populated from a dictionary, populated from a plist. The components populate, and I have a button that spins one component. The second button is meant to check answers (the first component has states which they try to match with capitals in the second component), but this is where it always crashes.
我正在尝试制作一个 2 组件选择器,从数组填充,从字典填充,从 plist 填充。组件填充,我有一个按钮可以旋转一个组件。第二个按钮用于检查答案(第一个组件的状态试图与第二个组件中的大写字母匹配),但这是它总是崩溃的地方。
The error I get is as follows:
我得到的错误如下:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*-[NSPlaceholderString initWithFormat:locale:arguments:]: nil argument'
*First throw call stack: (0x14b3022 0xeb3cd6 0x145ba48 0x145b9b9 0x916169 0x916ab4 0x3036 0x14b4e99 0x1914e 0x190e6 0xbfade 0xbffa7 0xbf266 0x3e3c0 0x3e5e6 0x24dc4 0x18634 0x139def5 0x1487195 0x13ebff2 0x13ea8da 0x13e9d84 0x13e9c9b 0x139c7d8 0x139c88a 0x16626 0x27ad 0x2715) terminate called throwing an exception(lldb)
* 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“*-[NSPlaceholderString initWithFormat:locale:arguments:]: nil argument”
*第一掷调用堆栈:(0x14b3022 0xeb3cd6 0x145ba48 0x145b9b9 0x916169 0x916ab4 0x3036 0x14b4e99 0x1914e 0x190e6 0xbfade 0xbffa7 0xbf266 0x3e3c0 0x3e5e6 0x24dc4 0x18634 0x139def5 0x1487195 0x13ebff2 0x13ea8da 0x13e9d84 0x13e9c9b 0x139c7d8 0x139c88a 0x16626 0x27ad 0x2715)终止叫做抛出异常(LLDB)
I'm not sure where I'm going wrong. I've done this with hard coded arrays, and it works fine. The code for the button is like so (please ignore the switch code, I haven't quite figured out how to make that work yet, I want the screen to turn green for correct answers and red for incorrect, but I'm more concerned with the app crashing!):
我不确定我哪里出错了。我已经使用硬编码数组完成了此操作,并且效果很好。按钮的代码是这样的(请忽略开关代码,我还没有完全弄清楚如何使它工作,我希望屏幕为正确的答案变成绿色,为不正确的答案变成红色,但我更关心应用程序崩溃!):
-(IBAction)checkAnswer; { NSInteger StateRow = [pickerCapital selectedRowInComponent:karrayStateComponent]; NSInteger CapitalRow =[pickerCapital selectedRowInComponent:karrayCapitalComponent];
-(IBAction)checkAnswer; { NSInteger StateRow = [pickerCapital selectedRowInComponent:karrayStateComponent]; NSInteger CapitalRow =[pickerCapital selectedRowInComponent:karrayCapitalComponent];
NSString *state = [arrayState objectAtIndex:StateRow];
NSString *capital = [arrayCapital objectAtIndex:CapitalRow];
NSLog (@"%i",[pickerCapital selectedRowInComponent:karrayCapitalComponent]);
NSLog(@"%i", [pickerCapital selectedRowInComponent:karrayStateComponent]);
NSString *Title = [[NSString alloc]initWithFormat:@"You have selected %@ as the capital of %@.", capital, state];
NSString *Message =[[NSString alloc]initWithFormat:lblMsg];
UIAlertView *alert =[[UIAlertView alloc]initWithTitle:Title message:Message delegate:@"" cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
if (StateRow == CapitalRow) {
lblMsg = @"You are correct!";
UIColor *myColor;
switch ([not sure what to use here]) {
case 0:
myColor = [UIColor greenColor];
break;
default:
break;
}
}
else {
lblMsg = @"Incorrect";
UIColor *myColor;
switch ([not sure what to use here]) {
case 0:
myColor = [UIColor redColor];
break;
default:
break;
}
}
}
}
If anyone can offer any sort of help on this, I would appreciate it greatly! Thank you!
如果有人可以为此提供任何帮助,我将不胜感激!谢谢!
回答by Smith
Two things to check:
要检查的两件事:
- Where is lblMsg declared or assigned a value?
- Are you sure capital and state are not nil from the calls to [arrayCapital objectAtIndex]?
- lblMsg 在哪里声明或赋值?
- 您确定从对 [arrayCapital objectAtIndex] 的调用中,capital 和 state 不是零吗?
Specifically: the NSString documentationstates "Important: Raises an NSInvalidArgumentException if format is nil." for initWithFormat.
具体来说:NSString 文档指出“重要:如果格式为零,则引发 NSInvalidArgumentException。” 对于 initWithFormat。