xcode 如何修复“未实施协议”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6488183/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:21:17  来源:igfitidea点击:

How to fix "Protocol Not Implemented"

objective-cxcodecocoa-touchprotocolscompiler-warnings

提问by user4951

When I make a new controller called TestController, I insert

当我创建一个名为 的新控制器时TestController,我插入

<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate,UISearchBarDelegate>

to the interface of Test.h

到Test.h的界面

When I run the app on the simulator, there are many issues that say "incomplete implementation" and "method in protocol not implemented"

当我在模拟器上运行应用程序时,有很多问题说“实现不完整”和“协议中的方法未实现”

I know some method is not implemented, but which one? How do I know what method I should add?

我知道有些方法没有实现,但哪一个?我怎么知道我应该添加什么方法?

回答by Steven Fisher

Show the issues navigator and expand one of the "method in protocol not implemented" issues. The first detail item will show you where the method is defined in the headers (and, thus, which @protocol it's in). Even better, though, the second detail will show you the name of the protocol.

显示问题导航器并展开“未实现的协议中的方法”问题之一。第一个详细信息项将向您显示方法在标题中的定义位置(以及它所在的@protocol)。不过,更好的是,第二个细节将向您显示协议的名称。

Switch to the Issues navigator. Ignore the Incomplete implementationmessage, and look at the Method in protocol not implementedmessage. You can expand this message to get more information that the compiler provided. The first detail is what you're looking for:

切换到问题导航器。忽略Incomplete implementation消息,查看协议未实现消息中的Method。您可以展开此消息以获取编译器提供的更多信息。第一个细节是您要查找的内容:

Method declared here

方法在这里声明

You can see here that I've forgotten to include a method for tableView:numberOfRowsInSection:. I can copy this declaration and paste it into my own code to start writing the method.

您可以在此处看到我忘记包含tableView:numberOfRowsInSection:. 我可以复制此声明并将其粘贴到我自己的代码中以开始编写该方法。

Note that there are three issues in my issue navigator. The third is another missing method, which can also be expanded and copied in the same way.

请注意,我的问题导航器中有三个问题。第三种是另一种缺失的方法,同样可以进行扩展和复制。

回答by Zev Eisenberg

There are two things I can think of which you can look for. First, look in your .hfile and see if you have any method prototypes that you may have forgotten to implement, or else implemented and then changed the implementation such that it no longer matched the prototype's method signature.

我能想到的有两件事你可以找找。首先,查看您的.h文件,看看您是否有任何您可能忘记实现的方法原型,或者是否实现了然后更改了实现,使其不再与原型的方法签名匹配。

Second, if the compiler isn't telling you which methods are missing, look at the documentation of UITableViewDelegate, UITableViewDataSource, etc. In the Tasks section, where it lists the various public class and instance methods, it will say "required method" next to any that need to be there. For example, if you class conforms to the UITableViewDataSourcedelegate protocol, -tableView:cellForRowAtIndexPath:and -tableView:numberOfRowsInSection:are required.

其次,如果编译器是不会告诉你哪些方法缺失,看的文件UITableViewDelegateUITableViewDataSource等等。在任务部分,它列出了各种公共类和实例方法,它会说“需要法”旁边的任何必须在那里。例如,如果您的类符合UITableViewDataSource委托协议,-tableView:cellForRowAtIndexPath:并且-tableView:numberOfRowsInSection:是必需的。

回答by jpswain

Check your projectname-Prefix.pch file. I got carried away there and imported something I shouldn't have, and that made it look like the one protocol I was implementing in that class was not implemented, when in fact it was. Just go ahead and make sure you aren't including any protocols there.

检查您的 projectname-Prefix.pch 文件。我在那里不知所措并导入了一些我不应该拥有的东西,这使得我在该类中实现的一个协议看起来没有实现,而实际上它是。继续并确保您没有在那里包含任何协议。

In my case it was because the file I shouldn't have imported was anotherprotocol file, that was not directly related to the class where I was getting the warning.

就我而言,这是因为我不应该导入的文件是另一个协议文件,它与我收到警告的类没有直接关系。

Also when you clean out your *-Prefix.pch file, make sure you close and re-open Xcode. I had to do that to get it to take.

此外,当您清除 *-Prefix.pch 文件时,请确保关闭并重新打开 Xcode。我必须这样做才能让它接受。

Hope this helps!!

希望这可以帮助!!

回答by Rashid

Check your implementation file to see if you have included the there. If you have that bit, you need to delete it.

检查您的实现文件,看看您是否在那里包含了。如果你有那个位,你需要删除它。