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
PS。我正在使用Xcode 6.1.1
3 个解决方案
#1
16
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现在支持通过“修复它”来符合协议方法。它会自动为您填写缺少的方法。这是在2017年WWDC上宣布的。
Update: This doesn't seem to work for protocols defined by you, unfortunately.
更新:遗憾的是,这似乎不适用于您定义的协议。
#2
22
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增加了自动修复丢失协议方法的能力;看到盖伊达赫的回答。
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 Enter then 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-click或Option-Cmd-click(它使用辅助编辑器窗口)来跳转到它的定义,这基本上是你可以复制并粘贴到代码中的所有方法的列表。你需要从那里整理一下,但不是很多。
#3
2
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 + F to fix all issues at once.
备用选项:您可以使用键盘快捷键:Ctrl + Option + Command + F立即修复所有问题。
#1
16
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现在支持通过“修复它”来符合协议方法。它会自动为您填写缺少的方法。这是在2017年WWDC上宣布的。
Update: This doesn't seem to work for protocols defined by you, unfortunately.
更新:遗憾的是,这似乎不适用于您定义的协议。
#2
22
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增加了自动修复丢失协议方法的能力;看到盖伊达赫的回答。
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 Enter then 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-click或Option-Cmd-click(它使用辅助编辑器窗口)来跳转到它的定义,这基本上是你可以复制并粘贴到代码中的所有方法的列表。你需要从那里整理一下,但不是很多。
#3
2
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 + F to fix all issues at once.
备用选项:您可以使用键盘快捷键:Ctrl + Option + Command + F立即修复所有问题。