如何在 XCode 4.2 中为协议自动生成存根?

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

How to auto generate stubs for protocols in XCode 4.2?

iosxcode

提问by rakete

Can Xcode 4.2 auto-generate me the stubs for the protocols i defined in the header-file?

Xcode 4.2 可以为我在头文件中定义的协议自动生成存根吗?

In this tutorial (http://kurrytran.blogspot.com/2011/10/ios-5-storyboard-and.html) in the note of point 4 the author says, Xcode will now auto generate the methods i need. Otherwise I did something wrong...

在本教程 (http://kurrytran.blogspot.com/2011/10/ios-5-storyboard-and.html) 的第 4 点注释中,作者说,Xcode 现在将自动生成我需要的方法。否则我做错了什么......

采纳答案by phi

From your question, I can't understand exactly what you need.

从您的问题中,我无法完全理解您的需求。

If you need boilerplate code like the -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPathimplementation that comes when you subclass UITableViewController, then you should create your own templates as Grouchal suggests. Also check a similar question I asked here.

如果您需要样板代码,例如在您创建-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath子类时出现的实现UITableViewController,那么您应该按照 Grouchal 的建议创建自己的模板。还要检查我在这里问的类似问题。

I think that the tutorial you link to refers to code sense, for example:

我认为您链接到的教程是指代码意义,例如:

  • In a header file, add a protocol like <UITextFieldDelegate>:
  • 在头文件中,添加如下协议<UITextFieldDelegate>

@interface FirstViewController : UIViewController <UITextFieldDelegate>

@interface FirstViewController : UIViewController <UITextFieldDelegate>

  • Save, and then in your .m file, you can see the new methods that you can implement just by typing a dash "-" and then pressing "Escape" on your keyboard. It helps if you type the first letters, for example "-tex" and then "Escape" will display the methods of the UITextFieldDelegate.
  • 保存,然后在您的 .m 文件中,您可以看到只需键入破折号“-”然后按键盘上的“Escape”即可实现的新方法。如果您输入第一个字母会有所帮助,例如“-tex”然后“Escape”将显示 UITextFieldDelegate 的方法。

Try the same with UITableViewDelegateand UITableViewDataSource, you will see that you'll get a similar result as the one at step 4of your tutorial (note that in the screenshot, the user has already typed "-tab" to get the list of methods).

UITableViewDelegateand进行相同的尝试UITableViewDataSource,您将看到您将获得与教程第 4 步中的结果相似的结果(请注意,在屏幕截图中,用户已键入“-tab”以获取方法列表)。

回答by JulianSymes

If you create a new class through the Xcode wizard ( i.e. File->New->File or cmd-N and select Objective-C class ) then if you pick an appropriate built-in superclass from those in the 'Subclass Of' dropdown, you will get a template implementation with the essential stubs implemented, and commented-out versions of other methods, that you can base your code on. I've used this successfully for subclasses of UITableViewController. Not quite in the league of the Eclipse stub generation, but better than nothing.

如果您通过 Xcode 向导创建一个新类(即 File->New->File 或 cmd-N 并选择 Objective-C class ),那么如果您从“Subclass Of”下拉列表中选择合适的内置超类,您将获得一个模板实现,其中包含已实现的基本存根,以及其他方法的注释版本,您可以将其作为代码的基础。我已经成功地将它用于 UITableViewController 的子类。不完全属于 Eclipse 存根一代,但总比没有好。

回答by PTBG

Check out Accessorizerif I'm not mistaken, it can do what you want.

如果我没记错的话,请查看Accessorizer,它可以满足您的需求。

回答by Grouchal

Firstly you might want to look into using templates to achieve your goals:

首先,您可能需要考虑使用模板来实现您的目标:

http://blog.highorderbit.com/2009/03/15/customizing-xcode-cocoa-touch-file-templates/

http://blog.highorderbit.com/2009/03/15/customizing-xcode-cocoa-touch-file-templates/

The tutorial you have references also mentions in point 4:

您参考的教程也在第 4 点中提到:

Also note, if the method is not auto-generating you're probably doing something wrong. That's the main way I know I'm doing something wrong, or the program crashes.

另请注意,如果该方法不是自动生成的,则您可能做错了什么。这是我知道我做错了什么或程序崩溃的主要方式。

This might indicate that you have not followed the previous steps 100%

这可能表明您没有 100% 地遵循前面的步骤