”“对象”可能无法响应“功能”警告

发布于 2024-10-11 04:10:42 字数 1176 浏览 7 评论 0原文

在过去的几周里,我终于从常规 C 进入了 Obj-C,并开始了我的第一个应用程序。我看过教程并阅读了一本书和很多网页,但我知道我才刚刚开始。不管怎样,从今晚的大部分时间到今天早上,我一直在努力让这段代码正常工作,现在它可以编译了,我有一些警告。我已经搜索并找到了类似问题的解决方案,但仍然没有骰子。我想做的是将 txt 文档组成的数组放入组合框中的弹出列表中。

AwesomeBoxList.h:

    #import <Cocoa/Cocoa.h>
@interface AwesomeBoxList : NSObject 
{
  IBOutlet NSComboBox *ComboBoz;
}
-(NSArray *) getStringzFromTxtz;
- (void) awesomeBoxList;
@end

AwesomeBoxList.m:

#import "AwesomeBoxList.h"

@implementation AwesomeBoxList

-(NSArray *)getStringzFromTxtz 
{
...
return combind;
}

- (void) awesomeBoxList
{
 [ComboBoz setUsesDataSource:YES];


 [ComboBoz setDataSource:

[ComboBoz getStringzFromTxtz]: //'NSComboBox' may not respond to 'getStringzFromTxtz'

[ComboBoz comboBox:(NSComboBox *)ComboBoz objectValueForItemAtIndex: 

 [ComboBoz numberOfItemsInComboBox:(NSComboBox *)ComboBoz]]];


        /*'NSComboBox' may not respond to '-numberOfItemsInComboBox:'
   'NSComboBox' may not respond to '-comboBox:objectValueForItemAtIndex:'
   'NSComboBox' may not respond to '-setDataSource:'
  */
 }

@end

所以,由于所有这些错误以及我对 Obj-C 的了解仍然很浅,我一定犯了某种 n00b 错误。

感谢您的帮助。

for the last couple of weeks I've finally gotten into Obj-C from regular C and have started my first app. I've watched tutorials and read through a book along with a lot of webpages, but I know I've only just begun. Anyway, for most of the night and this morning, I've been trying to get this code to work, and now that it will compile, I have a few warnings. I've searched and found similar problems with solutions, but still no dice. What I'm trying to do is put an array made from a txt document into the popup list in a combo box.

AwesomeBoxList.h:

    #import <Cocoa/Cocoa.h>
@interface AwesomeBoxList : NSObject 
{
  IBOutlet NSComboBox *ComboBoz;
}
-(NSArray *) getStringzFromTxtz;
- (void) awesomeBoxList;
@end

AwesomeBoxList.m:

#import "AwesomeBoxList.h"

@implementation AwesomeBoxList

-(NSArray *)getStringzFromTxtz 
{
...
return combind;
}

- (void) awesomeBoxList
{
 [ComboBoz setUsesDataSource:YES];


 [ComboBoz setDataSource:

[ComboBoz getStringzFromTxtz]: //'NSComboBox' may not respond to 'getStringzFromTxtz'

[ComboBoz comboBox:(NSComboBox *)ComboBoz objectValueForItemAtIndex: 

 [ComboBoz numberOfItemsInComboBox:(NSComboBox *)ComboBoz]]];


        /*'NSComboBox' may not respond to '-numberOfItemsInComboBox:'
   'NSComboBox' may not respond to '-comboBox:objectValueForItemAtIndex:'
   'NSComboBox' may not respond to '-setDataSource:'
  */
 }

@end

So, with all of these errors and my still shallow knowledge of Obj-C, I must be making some sort of n00b mistake.

Thanks for the help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

秉烛思 2024-10-18 04:10:42

这里似乎确实存在很大的混乱,主要体现在您的上一篇文章中
(4) 行代码。您知道“:”符号用于将参数传递给方法,而不是终止一行吗?您本质上是以一种毫无意义的方式将最后 4 行菊花链连接在一起。至于具体的警告,getStringzFromTxtz是你在AwesomeBoxList上定义的方法,而不是NSComboBox的方法。 numberOfItemsInComboBox: 和comboBox:objectValueForItemAtIndex: 是 NSComboBoxDataSource 协议 方法,旨在由您的类代表 NSComboBox 实现,而不是 NSComboBox 方法。我建议阅读一些有关“代表和协议”的内容。

详细说明:
为了让 AwesomeBoxList 类使用 NSComboBox,它需要向组合框提供组合框需要知道的信息。在 Apple Universe 中,此类情况通常使用委托设计模式来处理。具体来说,组合框需要知道它将显示多少个项目以及每个显示项目的对象表示。组合框在 NSComboBoxDataSource 协议。通过提供此信息,您将充当组合框的数据源。 调用来告诉组合框它应该遵循您的类的数据

[ComboBoz setDataSource:self];

您可以通过在 Interface Builder 中将自己设置为其 dataSource 属性或从 AwesomeBoxList 实现中的某个位置 。这将确保组合框调用类中的方法来用信息填充自身。 AwesomeBoxList 类应该具体实现两个方法,并且组合框将调用这两个方法:

- (id)comboBox:(NSComboBox *)aComboBox objectValueForItemAtIndex:(NSInteger)index {
    switch (index) {
        // Return some object that is represented by index in the combo box
    }
}

- (NSInteger)numberOfItemsInComboBox:(NSComboBox *)aComboBox {
    // return theNumberOfItemsInYourComboBox;
}

提供有意义的实现就是用数据填充组合框所需要做的全部工作。我不知道你想要什么的具体细节,但这就是你想要遵循的模式。希望能有更多帮助。

There does seem to be a generally large amount of confusion here, mostly manifested in your last
(4) line(s) of code. You are aware that the ':' symbol is used to pass arguments to methods, not terminate a line? You are essentially daisy chaining those last 4 lines together in a way that makes no sense. As for the specific warnings, getStringzFromTxtz is a method you defined on AwesomeBoxList, not a method of NSComboBox. numberOfItemsInComboBox: and comboBox:objectValueForItemAtIndex: are NSComboBoxDataSource Protocol methods, intended to be implemented by your class on behalf of an NSComboBox, not NSComboBox methods. I would recommend doing a bit of reading on Delegates and Protocols.

To elaborate:
In order for your AwesomeBoxList class to use an NSComboBox, it needs to provide information to the combo box that the combo box needs to know. In the Apple Universe, situations like this are typically handled with the Delegate Design Pattern. Specifically, the combo box needs to know how many items it will be showing as well as the object representation for each of the shown items. The combo box declares the interface for doing this in the NSComboBoxDataSource Protocol. By providing this info, you are acting as the datasource for the combo box. You can tell the combo box that it should defer to your class for its data by setting yourself as its dataSource property in Interface Builder or with a call to

[ComboBoz setDataSource:self];

from somewhere in your AwesomeBoxList's implementation. That will ensure that the combo box calls methods in your class to populate itself with info. There are two methods specifically that your AwesomeBoxList class should implement, and which will be called by the combo box:

- (id)comboBox:(NSComboBox *)aComboBox objectValueForItemAtIndex:(NSInteger)index {
    switch (index) {
        // Return some object that is represented by index in the combo box
    }
}

- (NSInteger)numberOfItemsInComboBox:(NSComboBox *)aComboBox {
    // return theNumberOfItemsInYourComboBox;
}

Providing a meaningful implementation is all you need to do to populate your combo box with data. I don't know the specifics of what you want here, but that is the pattern you want to follow. Hope that helps a bit more.

屋顶上的小猫咪 2024-10-18 04:10:42

好吧, getStringzFromTxtz 无法发送到 NSComboBox 实例,因为该方法是在 AwesomeBoxList 类中声明和定义的。该方法在运行时不会被找到。

另外,我认为您可能需要了解委托和数据源 - 其他方法是 NSComboBoxDataSource 协议的一部分。查看文档中的组合框编程指南以获取示例。

Well, the getStringzFromTxtz won't work being sent to an NSComboBox instance as that method is declared and defined in your AwesomeBoxList class. This method will not be found at runtime.

Also, I think you may need to get your head around delegates and data sources - the other methods are part of the NSComboBoxDataSource protocol. Check out the Combo Box Programming Guide in the docs for examples.

书间行客 2024-10-18 04:10:42

这些方法不存在。 有关更多信息,请参阅 NSComboBox 文档信息。

而不是:

[ComboBoz 
 comboBox:(NSComboBox *)ComboBoz 
 objectValueForItemAtIndex:[ComboBoz numberOfItemsInComboBox:(NSComboBox *)ComboBoz]];

尝试:

[[comboBox objectValues] objectAtIndex:[comboBox numberOfItems] - 1];

另外,请注意,平台约定是在 ivar 的开头使用小写字母。

Those methods do not exist. See the NSComboBox documentation for more information.

Instead of:

[ComboBoz 
 comboBox:(NSComboBox *)ComboBoz 
 objectValueForItemAtIndex:[ComboBoz numberOfItemsInComboBox:(NSComboBox *)ComboBoz]];

Try:

[[comboBox objectValues] objectAtIndex:[comboBox numberOfItems] - 1];

Also, note that the platform convention is to use a lower-case letter at the beginning of an ivar.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文