gpt4 book ai didi

macos - cocoa 菜单栏错误 [__NSCFNumber length] : unrecognized selector sent to instance

转载 作者:行者123 更新时间:2023-12-03 16:47:32 26 4
gpt4 key购买 nike

我想向菜单栏 cocoa 桌面应用程序添加一些数组元素

这是我的代码,有错误

NSMenu *menu =[[NSMenu alloc]initWithTitle:@"menu"];
NSMenuItem *itemTest = [[NSMenuItem alloc] initWithTitle:@"Test" action:@selector(menuActionTest:) keyEquivalent:@"t"];
NSMenuItem *itemQuit = [[NSMenuItem alloc] initWithTitle:@"Quit App" action:@selector(menuActionQuit:) keyEquivalent:@"q"];



// add to menu
[menu addItem:itemTest];
[menu addItem:[NSMenuItem separatorItem]];


// handle the array, and here is the problem
NSArray* someList = [self getArray];
if ([someList count]>0) {
for(NSString *title in someList) {
NSLog(@"begin of add %@",title);
// here is the problem code
[menu addItem: [[NSMenuItem alloc] initWithTitle:title action:@selector(menuActionHelloWorld:) keyEquivalent:@""]];
NSLog(@"end of add %@",title);
}
[someList release];
[menu addItem:[NSMenuItem separatorItem]];
}

// add other to menu
[menu addItem:itemQuit];


//Define status bar-------------
NSStatusBar *bar = [NSStatusBar systemStatusBar];
NSStatusItem *statusItem = [[bar statusItemWithLength:NSVariableStatusItemLength]retain];

NSImage *menuImage = [NSImage imageNamed:@"status_off.png"];
[menuImage setTemplate:YES];
[statusItem setImage:menuImage];

[statusItem setHighlightMode:YES];
[statusItem setMenu:menu];

//Release----------------------
[itemTest release];
[itemQuit release];
[menu release];

当我使用时

[menu addItem: [[NSMenuItem alloc] initWithTitle:title action:@selector(menuActionHelloWorld:) keyEquivalent:@""]];

应用程序可以很好地构建,但是当我单击菜单栏中的状态图标时,收到错误消息

2012-02-24 16:20:38.393 MyApp[1546:503] -[__NSCFNumber length]: unrecognized selector sent to instance 0x202ecc3
2012-02-24 16:20:38.394 MyApp[1546:503] -[__NSCFNumber length]: unrecognized selector sent to instance 0x202ecc3
2012-02-24 16:20:38.398 MyApp[1546:503] (
0 CoreFoundation 0x00007fff85ebbfc6 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff86c1cd5e objc_exception_throw + 43
2 CoreFoundation 0x00007fff85f482ae -[NSObject doesNotRecognizeSelector:] + 190
3 CoreFoundation 0x00007fff85ea8e73 ___forwarding___ + 371
4 CoreFoundation 0x00007fff85ea8c88 _CF_forwarding_prep_0 + 232
5 CoreFoundation 0x00007fff85e1f616 CFStringGetLength + 118
6 HIToolbox 0x00007fff8cb95d07 _Z12CheckForDashP8MenuDatathPKhPK10__CFString + 100
7 HIToolbox 0x00007fff8cb95c84 _Z26CleanupAfter1ItemInsertionP8MenuDatatPKhPK10__CFStringj + 41
8 HIToolbox 0x00007fff8cb95823 _Z31_InsertMenuItemTextWithCFStringP8MenuDataPK10__CFStringtjj + 204
9 AppKit 0x00007fff84ff8cfd -[NSCarbonMenuImpl _carbonMenuInsertItem:atCarbonIndex:] + 499
10 AppKit 0x00007fff8513c1a2 -[NSCarbonMenuImpl _privatePopulateCarbonMenu] + 298
11 AppKit 0x00007fff851c17a5 -[NSCarbonMenuImpl _populatePrivatelyIfNecessary] + 70
12 AppKit 0x00007fff851c174b -[NSCarbonMenuImpl _checkoutMenuRefWithToken:creating:populating:] + 298
13 AppKit 0x00007fff852edd46 -[NSCarbonMenuImpl _maximumSizeForScreen:] + 64
14 AppKit 0x00007fff8545f72b -[NSMenu size] + 35
15 AppKit 0x00007fff855517df +[NSStatusBarButtonCell popupStatusBarMenu:inRect:ofView:withEvent:] + 422
16 AppKit 0x00007fff85551b85 -[NSStatusBarButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 147
17 AppKit 0x00007fff850d2bde -[NSControl mouseDown:] + 786
18 AppKit 0x00007fff8509d6e0 -[NSWindow sendEvent:] + 6306
19 AppKit 0x00007fff85552598 -[NSStatusBarWindow sendEvent:] + 66
20 AppKit 0x00007fff8503616d -[NSApplication sendEvent:] + 5593
21 AppKit 0x00007fff84fcc1f2 -[NSApplication run] + 555
22 AppKit 0x00007fff8524ab88 NSApplicationMain + 867
23 RandomApp 0x00000001067390e2 main + 34
24 RandomApp 0x00000001067390b4 start + 52

我是Cocoa的新手,我尝试自己解决这个问题。

我发现了

[__NSCFNumber length]

所以我认为问题是从这一行出现的

 NSStatusItem *statusItem = [[bar statusItemWithLength:NSVariableStatusItemLength]retain];

此行是从苹果示例复制的:)它使状态项长度动态化,调整其内容的宽度。 NSStatusBar Class Reference

我想我设置了一个可变长度的 NSString 作为标题,因此应用程序无法决定其长度值应该是多少。

为了证明这一点,我将代码编写为

[menu addItem: [[NSMenuItem alloc] initWithTitle:@"HelloWorld" action:@selector(menuActionHelloWorld:) keyEquivalent:@""]];    

效果很好。

但我确实需要动态获取标题。

那么,我该如何修复我的代码?

谢谢!

编辑1:

谢谢

我添加

NSString *newTitle = [NSString stringWithFormat:@"%d",title];

对于我的代码,它有效。

所以我认为问题出在我的方法 getArray

这是我的 getArray 方法中的一些代码

NSMutableArray *List = [[NSMutableArray alloc] init];

for ( some conditions) {

// get something from a JSON result
NSString *title = [object valueForKeyPath:@"id"];

[List addObject:title];

}

return List;

在我看来,List 是一个由大量 NSString 元素组成的数组,但我认为我错了。我应该做什么来改进我的代码?好多了?

最佳答案

在我看来,您不小心传递了一个 NSNumber 作为您的标题。 (我猜测这是因为代码在添加 HelloWorlds 时工作正常,并且 lengthNSString 上的常用方法。)

我需要查看您的 getArray 实现才能确切地知道出了什么问题,但我敢打赌,如果您意外地知道其中有一个 NSNumber ,将会足以让你解决它。 (顺便说一句,将访问器简单命名为 array,而不是 getArray,这是 Cocoa 风格。)

关于macos - cocoa 菜单栏错误 [__NSCFNumber length] : unrecognized selector sent to instance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9427848/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com