我正在为mac编写秒表应用程序,目前正在处理'圈'功能。我将这些圈子放入表格视图中以便更好地组织。我正在使用数组控制器将事情放到桌面上。
基本上,我要做的是:
[arrayController addObject: [NSMutableDictionary dictionaryWithObjectsAndKeys:@"Lap 1",
@"lapNumber", nil]];
这很好,花花公子,但我希望能够使用表示圈数的整数来控制圈数,称为numLaps。因此,我的代码将是:
[arrayController addObject: [NSMutableDictionary dictionaryWithObjectsAndKeys:@"Lap %i",
numLaps, @"lapNumber", nil]];
然而,因为在nil之前有超过两个逗号,我认为程序搞砸了。我在控制台中抛出了以下内容,但我并不完全明白这意味着什么/如何解决它:
2013-09-03 16:52:31.515 Popup[3242:303] +[NSMutableDictionary dictionaryWithObjectsAndKeys:]: second object of each pair must be non-nil. Or, did you forget to nil-terminate your parameter list?
2013-09-03 16:52:31.519 Popup[3242:303] (
0 CoreFoundation 0x00007fff9800a0a6 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff9920b3f0 objc_exception_throw + 43
2 CoreFoundation 0x00007fff97fe8e31 +[NSDictionary dictionaryWithObjectsAndKeys:] + 433
3 Popup 0x00000001000035a0 -[PanelController btnLapWasClicked:] + 192
4 AppKit 0x00007fff96082a59 -[NSApplication sendAction:to:from:] + 342
5 AppKit 0x00007fff960828b7 -[NSControl sendAction:to:] + 85
6 AppKit 0x00007fff960827eb -[NSCell _sendActionFrom:] + 138
7 AppKit 0x00007fff96080cd3 -[NSCell trackMouse:inRect:ofView:untilMouseUp:] + 1855
8 AppKit 0x00007fff96080521 -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 504
9 AppKit 0x00007fff9607fc9c -[NSControl mouseDown:] + 820
10 AppKit 0x00007fff9607760e -[NSWindow sendEvent:] + 6853
11 AppKit 0x00007fff96073744 -[NSApplication sendEvent:] + 5761
12 AppKit 0x00007fff95f892fa -[NSApplication run] + 636
13 AppKit 0x00007fff95f2dcb6 NSApplicationMain + 869
14 Popup 0x0000000100001652 main + 34
15 Popup 0x0000000100001624 start + 52
)
任何想法如何以另一种方式实现我想要做的事情,不会混淆程序?
感谢。
答案 0 :(得分:0)
你应该使用现代的obj-c表示法。这使您能够以更自然的方式创建字典和数组。
NSDictionary *dic = @{@"Laps ": @(numLaps), @"someotherkey":@"anditswalue"};
在代码中,您显示的是键和值不是成对的。您必须始终插入对。 (有关详细参考,请参阅NSDictionary documentation。)
NSString *key = @"laps";
NSString *value = [NSStringWithFormat:@"Lap %i", numLaps];
[arrayController addObject: [NSMutableDictionary dictionaryWithObjectsAndKeys:
value, key, nil]];
此外,您需要考虑要在词典中存储的内容。在您的示例中,键和值不明显。