Auto-implement Swift protocol methods in Xcode

23,486

Solution 1

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.

Solution 2

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.

Solution 3

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

Share:
23,486
user3207838
Author by

user3207838

Updated on October 09, 2020

Comments

  • user3207838
    user3207838 over 3 years

    I just started with the swift language and coming from java it looks great so far. But I'm really missing something:

    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.

    Xcode doesn't seem to have this feature, or am I missing something?

    I found the accessorize plugin, but this seems only to work with Objective C..

    Is there a solution?

    PS. I'm using Xcode 6.1.1

  • chrisortman
    chrisortman over 8 years
    Do you know of any way to expand that autocomplete view so you can see the entire method signature?
  • marsbear
    marsbear over 8 years
    Or do you know how to keep on typing beyond e.g. tableView without the auto-completion failing to work? I haven't found a way to e.g. type tableView:willSelect... to jump to the end of the list to the proper method. You always have the full list of all tableView-relevant methods and have to scroll to the end...
  • andrewz
    andrewz over 8 years
    This solution doesn't work for me in XCode 7.3 or 7.0
  • Jase
    Jase over 7 years
    @marsbear you've probably already worked it out, but for any that come across this: just keep typing parts of the method signature, when I had typed tableviewcanperf, it had narrowed the autocomplete options to just tableView:canPerformAction
  • marsbear
    marsbear over 7 years
    @Jase Yes, thanks to the new Xcode that works now :)
  • user3207838
    user3207838 about 7 years
    Finally! That was long overdue!
  • Matt Gibson
    Matt Gibson about 7 years
    Indeed. Hurrah!
  • henrique
    henrique over 6 years
    Unfortunately not available to all protocols, Collection is one of them, bug maybe?
  • Steve Moser
    Steve Moser over 6 years
    If you see a red exclamation mark instead of a Fix-it then look for more information on the error by clicking on the disclosure triangle in the Issues Navigator. If you see something about 'Candidate is not a function' then remove or comment out that code first and then the Fix-it will appear.