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

时间:2021-09-25 12:40:52

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中可行吗?

10 个解决方案

#1


7  

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存档)。它还将生成带有自定义前缀的静态或#define字符串常量;copyWithZone:;如果你需要的话,还有其他的东西——都可以通过服务或工具栏通过简单的快捷方式实现。装饰的存档

#2


8  

I believe that Accessorizer will do what you want.

我相信配饰设计师会做你想做的。

#3


6  

Not the direсt answer, just hint:

不是direсt答案,只是提示:

Out of the box XCode can't.

XCode不能跳出框框。

But AppCode can.

但是本地可以。

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

它可以自动地做这些事情(当然,需要你的许可)。

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

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

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

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

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

#4


1  

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菜单脚本中发表了一篇文章

#5


1  

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将自动完成方法签名。这不是你想要的,但非常方便。

#6


1  

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中。

#7


1  

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

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

#8


0  

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的“创建@property, @synthesize & dealloc来自变量声明”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).
  • 在XCode (Shell脚本)上设置脚本,并为其分配快捷方式(例如CMD+5)。
  • Select lines from your interface file in which you want to generate the implementation, and press the hotkey.
  • 从要生成实现的接口文件中选择lines,然后按下热键。
  • Your .m will contain your selected methods.
  • 你的。m将包含你选择的方法。

#9


0  

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.

我知道这是一个老问题,但如果你想一直看到最新的定义,只要右键单击所讨论的类并跳转到定义。在这里,lyes所有当前的非弃用函数,所以您不需要依赖第三方来保持最新。

#10


0  

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;

#1


7  

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存档)。它还将生成带有自定义前缀的静态或#define字符串常量;copyWithZone:;如果你需要的话,还有其他的东西——都可以通过服务或工具栏通过简单的快捷方式实现。装饰的存档

#2


8  

I believe that Accessorizer will do what you want.

我相信配饰设计师会做你想做的。

#3


6  

Not the direсt answer, just hint:

不是direсt答案,只是提示:

Out of the box XCode can't.

XCode不能跳出框框。

But AppCode can.

但是本地可以。

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

它可以自动地做这些事情(当然,需要你的许可)。

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

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

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

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

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

#4


1  

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菜单脚本中发表了一篇文章

#5


1  

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将自动完成方法签名。这不是你想要的,但非常方便。

#6


1  

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中。

#7


1  

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

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

#8


0  

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的“创建@property, @synthesize & dealloc来自变量声明”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).
  • 在XCode (Shell脚本)上设置脚本,并为其分配快捷方式(例如CMD+5)。
  • Select lines from your interface file in which you want to generate the implementation, and press the hotkey.
  • 从要生成实现的接口文件中选择lines,然后按下热键。
  • Your .m will contain your selected methods.
  • 你的。m将包含你选择的方法。

#9


0  

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.

我知道这是一个老问题,但如果你想一直看到最新的定义,只要右键单击所讨论的类并跳转到定义。在这里,lyes所有当前的非弃用函数,所以您不需要依赖第三方来保持最新。

#10


0  

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;