Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto-implement Swift protocol methods in Xcode

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

like image 525
user3207838 Avatar asked Dec 06 '14 12:12

user3207838


People also ask

Can Swift protocol implementation?

In Swift, to use a protocol, other classes must conform to it. After we conform a class to the protocol, we must provide an actual implementation of the method.

How many protocols can a Swift class adopt?

Swift 4 allows multiple protocols to be called at once with the help of protocol composition.

What is difference between protocol and delegate in Swift?

We can say Protocol as a set of rules. That rules can be optional or required like we have to use in protocol. Delegates is a message passing technique in objective C and swift. An object has to take care of that message.

How do you implement a protocol?

Protocols are basically set of rules. The way to implement them is to first of all make a state machine diagram as it completely tells that what is going to be the current state and how the state is going to change on the basis of input and what output actions are going to be performed.


2 Answers

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.

like image 165
Matt Gibson Avatar answered Oct 02 '22 19:10

Matt Gibson


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.

like image 33
Guy Daher Avatar answered Oct 02 '22 19:10

Guy Daher