在XCode中自动实现Swift协议方法

时间:2022-09-14 09:28:12

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有一个很棒的特性,当我添加“实现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

3 个解决方案

#1


14  

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上宣布的。

在XCode中自动实现Swift协议方法

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.

NB:这个答案在Xcode版本9之前是正确的;在版本9中,苹果增加了自动修复丢失的协议方法的能力;看到人哒回答。

I just created an iOS project, and headed into my UIViewController.swift file. There I declared that it implemented UITableViewDelegate:

我刚创建了一个iOS项目,并进入了UIViewController。快速文件。在那里我声明它实现了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…",我看到适当的自动完成:

在XCode中自动实现Swift协议方法

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或opt -Cmd-click(它使用一个辅助编辑器窗口)来跳转到它的定义,它基本上是一个所有您可以复制和粘贴到您的代码中的方法的列表。你需要从那里整理一下,但不是很多。

#3


1  

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:

指示警告,以执行协议方法:

在XCode中自动实现Swift协议方法

By clicking on 'Fix' button, it has added all mandatory methods:

通过点击“Fix”按钮,它添加了所有的强制方法:

在XCode中自动实现Swift协议方法

#1


14  

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上宣布的。

在XCode中自动实现Swift协议方法

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.

NB:这个答案在Xcode版本9之前是正确的;在版本9中,苹果增加了自动修复丢失的协议方法的能力;看到人哒回答。

I just created an iOS project, and headed into my UIViewController.swift file. There I declared that it implemented UITableViewDelegate:

我刚创建了一个iOS项目,并进入了UIViewController。快速文件。在那里我声明它实现了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…",我看到适当的自动完成:

在XCode中自动实现Swift协议方法

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或opt -Cmd-click(它使用一个辅助编辑器窗口)来跳转到它的定义,它基本上是一个所有您可以复制和粘贴到您的代码中的方法的列表。你需要从那里整理一下,但不是很多。

#3


1  

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:

指示警告,以执行协议方法:

在XCode中自动实现Swift协议方法

By clicking on 'Fix' button, it has added all mandatory methods:

通过点击“Fix”按钮,它添加了所有的强制方法:

在XCode中自动实现Swift协议方法