Xcode 中是否有组合键来实现协议?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/194241/
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
Is there a key combination in Xcode to implement a Protocol?
提问by AidenMontgomery
In Visual Studio if I define a class to implement an interface e.g.
在 Visual Studio 中,如果我定义一个类来实现一个接口,例如
class MyObject : ISerializable {}
I am able to right click on ISerializable, select "Implement Interface" from the context menu and see the appropriate methods appear in my class definition.
我可以右键单击 ISerializable,从上下文菜单中选择“ Implement Interface”,然后查看相应的方法出现在我的类定义中。
class MyObject : ISerializable {
#region ISerializable Members
public void GetObjectData(SerializationInfo info,
StreamingContext context)
{
throw new NotImplementedException();
}
#endregion
}
Is there anything anything like this functionality available in Xcode on the Mac? I would like to be able to automatically implement Protocols in this way. Maybe with the optional methods generated but commented out.
Mac 上的 Xcode 中是否有类似此功能的功能?我希望能够以这种方式自动实现协议。也许使用生成但注释掉的可选方法。
采纳答案by David Grant
I have not seen that feature in Xcode. But it seems like someone could write a new user script called "Place Implementor Defs on Clipboard" that sits inside of Scripts > Code.
我还没有在 Xcode 中看到该功能。但似乎有人可以编写一个名为“在剪贴板上放置实施者定义”的新用户脚本,该脚本位于脚本 > 代码中。
You did not find this useful.
你没有发现这很有用。
回答by Clay
XCode currently does not support that kind of automation. But: an easy way to get your code bootstrapped with a protocol is to option-click the protocol name in your class declaration
XCode 目前不支持这种自动化。但是:使用协议引导代码的一种简单方法是在类声明中选择单击协议名称
@interface FooAppDelegate : NSObject <NSApplicationDelegate,
NSTableViewDelegate> {
to quickly open the .h file defining the protocol. From there, copy and paste the methods you're interested in. Those headers tend to be well-commented, which helps in determining which methods you can safely ignore.
快速打开定义协议的 .h 文件。从那里,复制并粘贴您感兴趣的方法。这些标题往往有很好的注释,这有助于确定您可以安全地忽略哪些方法。
回答by Chris Hanson
There is not currently such a refactoring in Xcode.
目前 Xcode 中没有这样的重构。
If you'd like it, please file an enhancement request.
如果您愿意,请提交增强请求。
回答by Randy Eppinger
I know this thread s a bit old, but I wondered the same thing and found this question.
我知道这个线程有点老,但我想知道同样的事情并发现了这个问题。
In my case, I'm defining a property in the interface (.h) and I want to synthesize it in the implementation (.m). I also need to implement methods defined in the interface. Yes, Xcode helps as others have mentioned, but modern IDEs offer these productivity enhancements for things we do frequently. It appears that this is still not a feature in Xcode 4.3.3. However, the feature is available in JetBrains' AppCode. I'm only dabbling with the trial, but it appears to only be possible one property or method at a time, not the whole interface like Visual Studio.
就我而言,我在接口 (.h) 中定义了一个属性,我想在实现 (.m) 中合成它。我还需要实现接口中定义的方法。是的,正如其他人提到的,Xcode 有帮助,但现代 IDE 为我们经常做的事情提供了这些生产力增强。看起来这仍然不是 Xcode 4.3.3 中的功能。但是,该功能在JetBrains 的 AppCode 中可用。我只是涉足试用,但似乎一次只能使用一个属性或方法,而不是像 Visual Studio 这样的整个界面。
回答by Harshit Gupta
check this plugin
检查这个插件
https://github.com/music4kid/FastStub-Xcode
https://github.com/music4kid/FastStub-Xcode
it does the thing that you are asking for and more.
它可以完成您所要求的事情,甚至更多。
回答by bithavoc
Xcode can help you per protocol method, lets say you have a protocol like this:
Xcode 可以帮助你每个协议方法,假设你有一个这样的协议:
@protocol PosterousWebsitesDelegate <NSObject>
- (void)PosterousWebsitesLoadSuccess:(PosterousWebsites*)websites;
@end
in the @implementation section of your .m file you can start writing the name of the method and pressing ESC key to autocomplete the signature of the method/selector:
在 .m 文件的 @implementation 部分,您可以开始编写方法的名称并按 ESC 键自动完成方法/选择器的签名:
-(void)Poste (...press ESC...)
Xcode will autocomplete a full signature of the @protocol method, pres TAB to confirm the code.
Xcode 将自动完成@protocol 方法的完整签名,按 TAB 键确认代码。
If you are really committing to learn OSX/iOS Development, I would recommend you to read "XCode 3 Unleashed", a book that really helped me to know Xcode as deep as I know VS :)
如果您真的致力于学习 OSX/iOS 开发,我建议您阅读“XCode 3 Unleashed”,这本书确实帮助我像了解 VS 一样深入了解 Xcode :)