ios 在 Xcode 中自动实现 Swift 协议方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27331588/
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
Auto-implement Swift protocol methods in Xcode
提问by user3207838
I just started with the swift language and coming from java it looks great so far. But I'm really missing something:
我刚开始使用 swift 语言,来自 java 到目前为止看起来很棒。但我真的错过了一些东西:
Eclipse had this great feature that when I added "implements XYZ" it provided me a function to automatically implement the necessary function stubs. This is a huge timesaver.
Eclipse 有一个很棒的功能,当我添加“implements XYZ”时,它为我提供了一个功能来自动实现必要的功能存根。这是一个巨大的时间节省。
Xcode doesn't seem to have this feature, or am I missing something?
Xcode 似乎没有这个功能,还是我遗漏了什么?
I found the accessorize plugin, but this seems only to work with Objective C..
我找到了 accessorize 插件,但这似乎只适用于 Objective C ..
Is there a solution?
有解决办法吗?
PS. I'm using Xcode 6.1.1
附注。我正在使用 Xcode 6.1.1
采纳答案by Guy Daher
Xcode 9
Xcode 9
Xcode 9 now supports conforming to protocol methods via a "fix it". It will automatically fill the missing methods for you. This was announced in WWDC 2017.
Xcode 9 现在支持通过“修复”来符合协议方法。它会自动为您填充缺失的方法。这是在 WWDC 2017 中宣布的。
Update:This doesn't seem to work for protocols defined by you, unfortunately.
更新:不幸的是,这似乎不适用于您定义的协议。
回答by Matt Gibson
NB: This answer is true for Xcode before version 9; in Version 9 Apple added the ability to automatically fix up missing protocol methods; see Guy Daher's answer.
注意:此答案适用于版本 9 之前的 Xcode;在第 9 版中,Apple 添加了自动修复缺少的协议方法的功能;见Guy Daher 的回答。
I just created an iOS project, and headed into my UIViewController.swift file. There I declared that it implemented UITableViewDelegate:
我刚刚创建了一个 iOS 项目,然后进入了我的 UIViewController.swift 文件。在那里我声明它实现了 UITableViewDelegate:
class ViewController: UIViewController, UITableViewDelegate {
Now when I go into the body of the class, and start typing "tab...", I see the appropriate autocompletions:
现在,当我进入课程主体并开始输入“tab...”时,我会看到相应的自动完成:
Hitting Enterthen inserts the function stub, with the cursor placed ready to code.
Enter然后点击插入函数存根,光标准备好进行编码。
That's just the way Xcode autocompletes protocols (or inherited class methods.) If you want to do all of them at once, I don't think it's possible, sadly. But you can always Cmd-click or Option-Cmd-click (which uses a secondary editor window) on the protocol name to jump to its definition, which is basically a list of all its methods you can copy and paste into your code. You'd need to tidy up a little from there, but not much.
这只是 Xcode 自动完成协议(或继承的类方法)的方式。如果你想一次完成所有这些,我认为这是不可能的,很遗憾。但你总是可以Cmd单击鼠标右键或Option-Cmd单击鼠标右键的协议名称(使用辅助编辑器窗口)跳转到它的定义,这基本上是所有的方法,你可以复制并粘贴到你的代码清单。你需要从那里整理一下,但不多。
回答by Krunal
Xcode 9, takes care of implementation of mandatory methods of Swift Datasource & Delegates.
Xcode 9,负责 Swift 数据源和委托的强制方法的实现。
Look at these snapshots, with example of UICollectionViewDataSource
:
看看这些快照,例如UICollectionViewDataSource
:
Indicating warning to implement protocol methods:
指示实现协议方法的警告:
By clicking on 'Fix' button, it has added all mandatory methods:
通过单击“修复”按钮,它添加了所有必需的方法:
Alternate Option:
You can use a keyboard short key: Ctrl+ Option+ Command+ Fto fix all issues at once.
替代方案:
您可以使用键盘快捷键:Ctrl+ Option+ Command+F解决所有的问题一次。