objective-c Xcode:可以为协议接口所需的方法自动创建存根吗?

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

Xcode: Possible to auto-create stubs for methods required by Protocol interface?

objective-cxcodeideinterfaceprotocols

提问by Eric Farraro

Coming from an Eclipse / Java background, one of my favorite features is the ability to quickly stub out all the methods required by an interface. In Eclipse, I can choose 'Override / implement' from the source menu to generate stub methods for any method of the Interface.

来自 Eclipse/Java 背景,我最喜欢的特性之一是能够快速删除接口所需的所有方法。在 Eclipse 中,我可以从源菜单中选择“覆盖/实现”来为接口的任何方法生成存根方法。

I'd like to do the same thing in Objective-C. For instance, if I declare a class that implements the 'NSCoding' protocol, I'd like to have Xcode automatically generate the methods required to implement this Protocol. It's frustrating to have to look-up and then copy/paste the signatures of the required methods every Protocol that I'm trying to implement.

我想在 Objective-C 中做同样的事情。例如,如果我声明一个实现“NSCoding”协议的类,我希望 Xcode 自动生成实现该协议所需的方法。必须查找然后复制/粘贴我试图实现的每个协议所需方法的签名,这令人沮丧。

I've been trying for awhile to find out if this is possible, but haven't found anything promising yet. Is this possible in XCode?

我一直在尝试找出这是否可行,但还没有发现任何有希望的东西。这在 XCode 中可能吗?

采纳答案by Kevin

Accessorizer will write the encode and decode methods for ivars passed to it (NSCoding protocol and for NSDocument archiving). It will also generate string constants either static or #define with a custom prefix; copyWithZone:; and other things if you need - all from a simple shortcut via Services or from the toolbar. Accessorizer keyed archiving

Accessorizer 将为传递给它的 ivars 编写编码和解码方法(NSCoding 协议和 NSDocument 归档)。它还将生成带有自定义前缀的 static 或 #define 字符串常量;copyWithZone:; 以及其他需要的东西 - 所有这些都来自通过服务或工具栏的简单快捷方式。 配件键控归档

回答by jessecurry

I believe that Accessorizerwill do what you want.

我相信Accessorizer会做你想做的。

回答by skywinder

Not the direсt answer, just hint:

不是直接的答案,只是提示:

Out of the box XCode can't.

开箱即用的 XCode 不能。

But AppCodecan.

但是AppCode可以。

It can do this things automatically (with your permission, of course).

它可以自动执行这些操作(当然需要您的许可)。

If some methods of protocol marked as @required- AppCodewill highlight the implementation and suggest to implement this methods.

如果协议的某些方法标记为@required- AppCode将突出显示该实现并建议实现该方法。

example

例子

@optionalmethods also available to implement automatically (shortcut: control+ I).

@optional方法也可自动实现(快捷方式:control+ I)。

回答by Brad

I'm also looking for a way to generate method stubs for the protocols in my header file. I checked out Accessorizer and it looks to be a handy tool but unless I missed something I didn't find a way to get it to generate method stubs for a protocol.

我也在寻找一种方法来为我的头文件中的协议生成方法存根。我检查了 Accessorizer,它看起来是一个方便的工具,但除非我错过了一些东西,否则我没有找到让它为协议生成方法存根的方法。

Eric, If you found another solution please post what you found. It's amazing to me that XCode doesn't already have this built into the IDE.

埃里克,如果您找到了其他解决方案,请发布您找到的内容。令我惊讶的是,XCode 尚未将其内置到 IDE 中。

回答by Niels Castle

Your can create scripts for the scripting menu item in AppleScript, Perl, Python, Ruby, or any other scripting language that go in the scripting menu.

您可以使用 AppleScript、Perl、Python、Ruby 或脚本菜单中的任何其他脚本语言为脚本菜单项创建脚本。

Your could place the insertion point in the .m file and have the script look up the corresponding .h file. Locate the protocols supported and so forth...

您可以将插入点放在 .m 文件中,并让脚本查找相应的 .h 文件。找到支持的协议等等...

MacTech ran an article in 2007 Xcode Menu Scripts

MacTech 在 2007 Xcode Menu Scripts 中跑了一篇文章

回答by Lily Ballard

Xcode 3.2 will autocomplete known method implementations. In other words, if the method is declared somewhere (for example, in a protocol), when you start to type it in a .m file, Xcode 3.2 will autocomplete the method signature for you. This isn't quite what you asked for, but it is awfully handy.

Xcode 3.2 将自动完成已知的方法实现。换句话说,如果方法在某处声明(例如,在协议中),当您开始在 .m 文件中键入它时,Xcode 3.2 将为您自动完成方法签名。这不是你所要求的,但它非常方便。

回答by pille

Since the accepted answer's given link does not work anymore (and is redirected to an ad), here'sanother good explanation on how to use accessorizer to create protocol method stubs.

由于接受的答案的给定链接不再有效(并被重定向到广告),这里是关于如何使用访问器创建协议方法存根另一个很好的解释。

回答by Mark McCorkle

I know this is an old question but if you'd like to always see the latest definitions just right click on the class in question and Jump to Definition. Here lyes all the current non-deprecated functions so you aren't relying on a 3rd party to stay up to date.

我知道这是一个老问题,但如果您想始终看到最新的定义,只需右键单击相关类并跳转到定​​义。此处列出了所有当前未弃用的功能,因此您无需依赖第 3 方来保持最新状态。

回答by AlfredBaudisch

Based on AllanCraig's "Create @property, @synthesize & dealloc from Variable Declaration" ruby script, I made one to generate implementation stubs from interface ones: http://pastebin.com/4T2LTBh6

基于 AllanCraig 的“Create @property, @synthesize & dealloc from Variable Declaration”ruby 脚本,我制作了一个从接口生成实现存根:http: //pastebin.com/4T2LTBh6

How to use?

如何使用?

  • Setup the script on your XCode (Shell Script) and assign a shortcut for it (e.g. CMD+5).
  • Select lines from your interface file in which you want to generate the implementation, and press the hotkey.
  • Your .m will contain your selected methods.
  • 在您的 XCode(Shell 脚本)上设置脚本并为其指定一个快捷方式(例如 CMD+5)。
  • 从接口文件中选择要在其中生成实现的行,然后按热键。
  • 您的 .m 将包含您选择的方法。

回答by Saleh Enam Shohag

In My case Below style helps me much, In a sense solved my problem.

在我的情况下,下面的样式对我有很大帮助,在某种意义上解决了我的问题。

Suppose you have following method declaration:

假设您有以下方法声明:

+(DBManager*)getSharedInstance;

From Implementation file you start typing +ge and xcode will automatically choose method

从实现文件你开始输入 +ge 和 xcode 将自动选择方法

+(DBManager*)getSharedInstance;