在 XCode 中将委托或协议的方法添加到 .m 文件的最有效方法是什么?

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

What is the most efficient way in XCode to add a delegate's or protocol's methods to the .m file?

xcodeideinterfacedelegatesprotocols

提问by craig

When one implements an interface (equivalent to a protocol in Objective-C) in the .Net environment, the IDE automatically adds the properties and methods that need to be implemented to the class's file. Is there a setting that will result in a similar behavior in the Xcode environment? Will it do the same for a delegate?

当在 .Net 环境中实现接口(相当于 Objective-C 中的协议)时,IDE 会自动将需要实现的属性和方法添加到类文件中。是否有设置会导致 Xcode 环境中的类似行为?它会为代表做同样的事情吗?

At this point, I find myself copying/pasting the protocol/delegate's methods from Apple's online documentation.

在这一点上,我发现自己从 Apple 的在线文档中复制/粘贴了协议/委托的方法。

回答by Karel Bílek

sorry for late comment, I use this neat trick.

抱歉,评论晚了,我使用了这个巧妙的技巧。

For example, your class is named "MyClass", you want it to have protocol NSTableDataSource. What you do is write

例如,您的类名为“MyClass”,您希望它具有协议 NSTableDataSource。你要做的是写

@interface MyClass : NSObject <NSTableDataSource> 
{ 
   ... usual stuff here ...
@end

then, you right-click on NSTableDataSource, click on "Jump to definition".... and you can copy it from there.

然后,您右键单击 NSTableDataSource,单击“跳转到定义”.... 然后您可以从那里复制它。

If you want it to be delegate of, let's say again, NSTableView, you just name the protocol NSTableViewDelegate (this is an actual protocol name!), right click on it, click on "Jump to definition" - and you have it there, you just have to ignore those preprocessor marks everywhere.

如果你希望它是的委托,让我们再说一次,NSTableView,你只需将协议命名为 NSTableViewDelegate(这是一个实际的协议名称!),右键单击它,单击“跳转到定义” - 你就在那里,你只需要在任何地方忽略那些预处理器标记。

It's maybe not as easy as with for example Java interfaces and NetBeans, but it's not significantly slower.

它可能不像 Java 接口和 NetBeans 那样容易,但并没有明显变慢。

回答by cleexiang

Instead of xCode, you can use appCode, "Generate" feature is usefull

您可以使用 appCode 代替 xCode,“生成”功能很有用

回答by Ben Stiglitz

Copy-and-paste is the fastest way to do it with Xcode 3.1 as available online. A little script to parse a header and spit out protocol defs would be pretty cool, though…

复制和粘贴是使用 Xcode 3.1 在线提供的最快方法。一个解析标头并吐出协议定义的小脚本会很酷,不过……