Auto-implement Swift protocol methods in Xcode Auto-implement Swift protocol methods in Xcode ios ios

Auto-implement Swift protocol methods in Xcode


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.

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

class ViewController: UIViewController, UITableViewDelegate {

Now when I go into the body of the class, and start typing "tab...", I see the appropriate autocompletions:

Xcode autocompletions

Hitting Enter then inserts the function stub, with the cursor placed ready to code.

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 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.

enter image description here

Update: This doesn't seem to work for protocols defined by you, unfortunately.


Xcode 9, takes care of implementation of mandatory methods of Swift Datasource & Delegates.

Look at these snapshots, with example of 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

Alternate Option:
You can use a keyboard short key: Ctrl + Option + Command + F to fix all issues at once.

enter image description here