xcode 如何让Xcode自动符合协议

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

How to make Xcode automatically conform to a protocol

iosxcodeswiftinterfacecode-generation

提问by Sweeper

When I use a prototype table view, I always have to conform to the protocol TableViewDataSource. I always forget what methods I need to implement, so I have to look at the source of the protocol every time. This is really time consuming.

当我使用原型表视图时,我总是要遵守协议TableViewDataSource。总是忘记需要实现什么方法,所以每次都要看协议的来源。这真的很费时间。

I think Xcode must have a feature that automatically implements the needed methods for you, right? Just like IntelliJ IDEA, Eclipse, and Visual Studio.

我认为 Xcode 必须具有自动为您实现所需方法的功能,对吗?就像 IntelliJ IDEA、Eclipse 和 Visual Studio。

I want to know where can I find this feature. If there's isn't, is there a workaround for this? At least I don't have to open the source code of the protocol each time I conform to it.

我想知道在哪里可以找到这个功能。如果没有,是否有解决方法?至少我不用每次遵守协议的源代码都去打开。

If you don't understand what I mean, here's some code:

如果你不明白我的意思,这里有一些代码:

I have a protocol

我有一个协议

protocol Hello {
    func doStuff ()
}

When I conform to it,

当我符合它时,

class MyClass: Hello {

}

I often don't remember the names of the methods that I need to implement. If Xcode has a feature that turns the above code, into this:

我经常不记得我需要实现的方法的名称。如果 Xcode 有一个功能可以把上面的代码变成这样:

class MyClass: Hello {
    func doStuff () {
        code
    }
}

Now you understand what I mean? I just want to ask where to find such a feature.

现在你明白我的意思了吗?我只想问在哪里可以找到这样的功能。

采纳答案by Hamza Ansari

Well if i understood your problem then here is a workaround:

好吧,如果我理解您的问题,那么这里有一个解决方法:

try to define methods with protocol as prefix like here hellothen you'll not have to remember the methods just start typing protocol name and XCODE will prompt you with all available methods see here:

尝试使用协议作为前缀定义方法,就像这里hello那样,你不必记住这些方法,只需开始输入协议名称,XCODE 就会提示你所有可用的方法,请参见此处:

enter image description here

在此处输入图片说明

And if you want autocomplete protocol try Snippets

如果你想要自动完成协议试试 Snippets

回答by Amjad Tubasi

Xcode 9: you can add missing Protocol Requirements by add new shortcut to your Key Bindings Set

Xcode 9:您可以通过向键绑定集添加新的快捷方式来添加缺少的协议要求

  1. From the top Xcode menu (top left) choose Preferences.
  2. Choose Key Binding.
  3. Search about protocol. you will find Command called Refactor -> Add Missing Protocol Requirements
  4. Press on Key column then add your shortcut. ex: cmd + shift + M
  1. 从顶部 Xcode 菜单(左上角)中选择 Preferences。
  2. 选择键绑定。
  3. 搜索协议。你会发现命令叫Refactor -> Add Missing Protocol Requirements
  4. 按 Key 列,然后添加您的快捷方式。 ex: cmd + shift + M

Nowyou can add missing Protocol Requirements by clicking on class name name (or his extension) then press your shortcut

现在您可以通过单击类名名称(或其扩展名)来添加缺少的协议要求,然后按您的快捷方式

enter image description here

在此处输入图片说明

回答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:

指示实现协议方法的警告:

enter image description here

在此处输入图片说明

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

通过单击“修复”按钮,它添加了所有必需的方法:

enter image description here

在此处输入图片说明

回答by Maschina

Very similar to Amjad's post, Xcode 9/10 brings the functionality to add missing protocol requirements right from within the code editor.

与 Amjad 的帖子非常相似,Xcode 9/10 带来了直接从代码编辑器中添加缺少的协议要求的功能。

Just "right click" on the class name: "Refactor" --> "Add Missing Protocol Requirements" enter image description here

只需在类名上“右键单击”:“重构”-->“添加缺少的协议要求” 在此处输入图片说明

回答by chedabob

Xcode won't do it for you.

Xcode 不会为你做这件事。

If you look at the documentation for the protocol, it's clearly marked which functions you have to implement:

如果您查看协议的文档,它会清楚地标明您必须实现哪些功能:

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDataSource_Protocol/

https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITableViewDataSource_Protocol/